note 87757 added to ref.array
| From: | crashrox at gmail dot com | Date: | Fri, 19 Dec 2008 17:51:01 +0000 |
| Subject: | note 87757 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148353@lists.php.net to get a copy of this message | ||
Recursive multidimensional array flatten using SPL
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
)
----
Server IP: 209.41.74.194
Probable Submitter: 74.9.71.138
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/87757
Del: integrated -- https://master.php.net/note/delete/87757/integrated
Del: useless -- https://master.php.net/note/delete/87757/useless
Del: bad code -- https://master.php.net/note/delete/87757/bad+code
Del: spam -- https://master.php.net/note/delete/87757/spam
Del: non-english -- https://master.php.net/note/delete/87757/non-english
Del: in docs -- https://master.php.net/note/delete/87757/in+docs
Del: other reasons-- https://master.php.net/note/delete/87757
Reject -- https://master.php.net/note/reject/87757
Search -- https://master.php.net/manage/user-notes.php