Skip to content

Instantly share code, notes, and snippets.

@jan-j
Last active July 17, 2019 21:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-j/f86ab6a9935b22469b43 to your computer and use it in GitHub Desktop.
Save jan-j/f86ab6a9935b22469b43 to your computer and use it in GitHub Desktop.
PHP array flatmap - array_flatmap function
/**
* Applies the callback to the elements of the given arrays, then merge all results to one array
*
* @param callable $callback Callback function to run for each element in each array.
* @param array $array1 An array to run through the callback function.
* @param array $array,... Variable list of array arguments to run through the callback function.
* @return array Returns an array containing all the elements of array1 after
* applying the callback function to each one, casting them to arrays and merging together.
*/
function array_flatmap()
{
$args = func_get_args();
$mapped = array_map(function ($a) {
return (array)$a;
}, call_user_func_array('array_map', $args));
return count($mapped) === 0 ? array() : call_user_func_array('array_merge', $mapped);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment