Last active
July 7, 2020 06:58
-
-
Save ameen-sarsour/c1049aeae40d24eba7ed6b350fc44c4f to your computer and use it in GitHub Desktop.
This Code to flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
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
<?php | |
/** | |
* @param array $array | |
* @return array Array in fal | |
*/ | |
function flat(Array $array){ | |
$result = []; | |
foreach($array as $item){ | |
if(is_array($item)){ | |
$result = array_merge($result, flat($item)); | |
}else{ | |
$result[] = $item; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment