Skip to content

Instantly share code, notes, and snippets.

@acabouet
Created November 7, 2017 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acabouet/3b8c48cc045248686bda985a726545b4 to your computer and use it in GitHub Desktop.
Save acabouet/3b8c48cc045248686bda985a726545b4 to your computer and use it in GitHub Desktop.
a simple PHP function to flatten an arbitrarily nested array.
$a = array('a', 'b', array(array(array('1'), '2', '3')), array(array('xyz')));
function make_flat($array,$result) {
for($i = 0; $i <= count($array); $i++) {
if(is_array($array[$i])) {
$result = make_flat($array[$i], $return);
}
else {
if(isset($array[$i])) {
$result[] = $array[$i];
}
}
}
return $result;
}
$res = make_flat($a, array());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment