PHP 5.4.36 Released

iterator_to_array

(PHP 5 >= 5.1.0)

iterator_to_arrayCopia o iterador em um array

Descrição

array iterator_to_array ( IteratorAggregate $iterator [, bool $use_keys ] )
Aviso

Esta função não está documentada; somente a lista de argumentos está disponível.

Conta os elementos em um iterador.

Parâmetros

iterator

O iterador a ser contado.

use_keys

Valor Retornado

O número de elementos em iterator.

add a note add a note

User Contributed Notes 3 notes

up
3
jerome at yazo dot net
6 years ago
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"
)
up
1
joksnet at gmail dot com
1 month ago
To generate an deep array from nested iterators:

<?php
function iterator_to_array_deep(\Traversable $iterator, $use_keys = true) {
   
$array = array();
    foreach (
$iterator as $key => $value) {
        if (
$value instanceof \Iterator) {
           
$value = iterator_to_array_deep($value, $use_keys);
        }
        if (
$use_keys) {
           
$array[$key] = $value;
        } else {
           
$array[] = $value;
        }
    }
    return
$array;
}
?>

I use it to test an iterator: https://gist.github.com/jm42/cb328106f393eeb28751
up
0
chad 0x40 herballure 0x2e com
6 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.
To Top