note 87757 deleted from class.recursiveiteratoriterator by salathe
| From: | salathe@php.net | Date: | Mon, 24 Aug 2020 12:58:41 +0000 |
| Subject: | note 87757 deleted from class.recursiveiteratoriterator by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215019@lists.php.net to get a copy of this message | ||
Note Submitter: crashrox at gmail dot com
----
Recursive multidimensional array flatten using SPL
<?php
function array_flatten_recursive($array) {
if($array) {
$flat = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST) as $key=>$value) {
if(!is_array($value)) {
$flat[] = $value;
}
}
return $flat;
} else {
return false;
}
}
$array = array(
'A' => array('B' => array( 1, 2, 3, 4, 5)),
'C' => array( 6,7,8,9)
);
print_r(array_flatten_recursive($array));
?>
-- Returns:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)