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
)
La classe RecursiveIteratorIterator
Introduction
...
Synopsis de la classe
RecursiveIteratorIterator
/* Méthodes */
}Sommaire
- RecursiveIteratorIterator::current — Accède à la valeur de l'élément courant
- RecursiveIteratorIterator::getDepth — Récupère la profondeur courante de la récursivité de l'itérateur
- RecursiveIteratorIterator::getSubIterator — L'itérateur secondaire actif courant
- RecursiveIteratorIterator::key — Accède à la clé courante
- RecursiveIteratorIterator::next — Déplace l'itérateur à la position suivante
- RecursiveIteratorIterator::rewind — Replace l'itérateur au début
- RecursiveIteratorIterator::valid — Vérifie si la position courante est valide
RecursiveIteratorIterator
crashrox at gmail dot com
19-Dec-2008 09:51
19-Dec-2008 09:51
