Skip to content

Instantly share code, notes, and snippets.

@meglio
Created September 7, 2011 08:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meglio/1200034 to your computer and use it in GitHub Desktop.
Save meglio/1200034 to your computer and use it in GitHub Desktop.
yeld in php (for arrays only)
<?php
namespace core\utils;
/**
* Loops over an array, calls expression on each its element and collects results into new array.
* @param mixed $array
* @param callback $expression Expression function taking $key and $value parameters.
* @return array
*/
function yeld($array, $expression)
{
$result = array();
foreach($array as $key => $value)
$result[$key] = $expression($key, $value);
return $result;
}
/**
* Joins all elements of $array with applying formatter and using separator string.
* @param mixed $array
* @param callback $formatter
* @param string $separator
* @return string
*/
function joinFormatted($array, $separator, $formatter)
{
return implode($separator, yeld($array, function($key, $value)use($formatter) { return $formatter($key, $value); }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment