Skip to content

Instantly share code, notes, and snippets.

@filjoseph1989
Last active July 17, 2016 23:27
Show Gist options
  • Save filjoseph1989/92565fbd93c5e891ca8f2b85bfe81ec1 to your computer and use it in GitHub Desktop.
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].
<?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