Last active
July 17, 2019 21:10
-
-
Save jan-j/f86ab6a9935b22469b43 to your computer and use it in GitHub Desktop.
PHP array flatmap - array_flatmap function
This file contains 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
/** | |
* 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