Skip to content

Instantly share code, notes, and snippets.

@ameen-sarsour
Last active July 7, 2020 06:58
Show Gist options
  • Save ameen-sarsour/c1049aeae40d24eba7ed6b350fc44c4f to your computer and use it in GitHub Desktop.
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].
<?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