Let's say I have an array like this
Array
(
[0] => 123
[180] => Array
(
[400] => Array
(
[0] => 474
[1] => 395
[2] => 994
[3] => 365
)
)
[1] => 144
[2] => 119
)
I would like to go though this array and generate a one dimensional array that contains all the numbers. In the loop, if we get an array, then the number is the corresponding array key.
I did something like this but it doesn't work:
function flatten_array($data) {
$result = array();
foreach ($data as $key => $value) {
if(is_array($value)) {
$result[] = $key;
flatten_array($value);
} else {
$result[] = $value;
}
}
return $result;
}
The resulting array I should get should be this one:
$result = Array
(
[0] => 123
[1] => 180
[2] => 400
[3] => 474
[4] => 395
[5] => 994
[6] => 365
[7] => 144
[8] => 119
)
Any help will be appreciated thanks.