Created
September 7, 2011 08:13
-
-
Save meglio/1200034 to your computer and use it in GitHub Desktop.
yeld in php (for arrays only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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