Last active
July 17, 2016 23:27
-
-
Save filjoseph1989/92565fbd93c5e891ca8f2b85bfe81ec1 to your computer and use it in GitHub Desktop.
Flatten a given 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 hidden or 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 | |
function flatten_array ( $temp = array(), $flatten=array() ) { | |
foreach ($temp as $key => $value) { | |
if (is_array($value)) { | |
$flatten = flatten_array ($value, $flatten); | |
} else { | |
$flatten[] = $value; | |
} | |
} | |
return $flatten; | |
} | |
#Example: | |
# Given | |
# [[1,2,[3]],4] are same structure below | |
$temp = array( | |
array( | |
'1', | |
'2', | |
array('3') | |
), | |
'4' | |
); | |
$flatten = flatten_array ($temp); | |
var_dump($flatten); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment