Using the boolean param :
<?php
$first = new ArrayIterator( array('k1' => 'a' , 'k2' => 'b', 'k3' => 'c', 'k4' => 'd') );
$second = new ArrayIterator( array( 'k1' => 'X', 'k2' => 'Y', 'Z' ) );
$combinedIterator= new AppendIterator();
$combinedIterator->append( $first );
$combinedIterator->append( $second );
var_dump( iterator_to_array($combinedIterator, false) );
?>
will output :
array(7) (
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "X"
[5]=>
string(1) "Y"
[6]=>
string(1) "Z"
)
<?php
var_dump( iterator_to_array($combinedIterator, true) );
?>
will output (since keys would merge) :
array(5) (
["k1"]=>
string(1) "X"
["k2"]=>
string(1) "Y"
["k3"]=>
string(1) "c"
["k4"]=>
string(1) "d"
[0]=>
string(1) "Z"
)
iterator_to_array
(PHP 5 >= 5.1.0)
iterator_to_array — Copia el iterador en un array
Descripción
Copia los elementos de un iterador en un array.
Parámetros
-
iterator -
El iterador a ser copiado.
-
use_keys -
Si se desea usar las claves del elemento iterador como un índice.
Valores devueltos
Un array que contiene los elementos del iterator.
Historial de cambios
| Versión | Descripción |
|---|---|
| 5.2.1 |
Añadido el parámetro use_keys.
|
Ejemplos
Ejemplo #1 Ejemplo de iterator_to_array()
<?php
$iterator = new ArrayIterator(array('recipe'=>'panqueques', 'huevo', 'leche', 'harina'));
var_dump(iterator_to_array($iterator, true));
var_dump(iterator_to_array($iterator, false));
?>
El resultado del ejemplo sería:
array(4) {
["recipe"]=>
string(8) "panqueques"
[0]=>
string(3) "huevo"
[1]=>
string(4) "leche"
[2]=>
string(5) "harina"
}
array(4) {
[0]=>
string(8) "panqueques"
[1]=>
string(3) "huevo"
[2]=>
string(4) "leche"
[3]=>
string(5) "harina"
}
jerome at yazo dot net ¶
4 years ago
chad 0x40 herballure 0x2e com ¶
5 years ago
The use_keys parameter was added in one of the 5.2.x releases; it defaults to TRUE. This matches the behavior in PHP 5.1.6, which lacks this parameter.
