Guest User

Untitled

a guest
Jan 22nd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. <?php
  2.  
  3. namespace core\utils;
  4.  
  5. /**
  6. * Loops over an array, calls expression on each its element and collects results into new array.
  7. * @param mixed $array
  8. * @param callback $expression Expression function taking $key and $value parameters.
  9. * @return array
  10. */
  11. function yeld($array, $expression)
  12. {
  13. $result = array();
  14. foreach($array as $key => $value)
  15. $result[$key] = $expression($key, $value);
  16. return $result;
  17. }
  18.  
  19. /**
  20. * Joins all elements of $array with applying formatter and using separator string.
  21. * @param mixed $array
  22. * @param callback $formatter
  23. * @param string $separator
  24. * @return string
  25. */
  26. function joinFormatted($array, $separator, $formatter)
  27. {
  28. return implode($separator, yeld($array, function($key, $value)use($formatter) { return $formatter($key, $value); }));
  29. }
Add Comment
Please, Sign In to add comment