iterator_to_array

(PHP 5 >= 5.1.3)

iterator_to_array — Copy the iterator into an array

Описание

array iterator_to_array ( IteratorAggregate $iterator [, bool $use_keys ] )
Внимание

К настоящему времени эта функция еще не была документирована; для ознакомления доступен только список аргументов.

Count the elements in an iterator.

Список параметров

iterator

The iterator being counted.

use_keys

Возвращаемые значения

The number of elements in iterator .

Коментарии

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.
2008-03-27 08:23:42
http://php5.kiev.ua/manual/ru/function.iterator-to-array.html
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_dumpiterator_to_array($combinedIteratorfalse) );

?>

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($combinedIteratortrue) );

?>

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"
)
2008-12-10 00:42:37
http://php5.kiev.ua/manual/ru/function.iterator-to-array.html
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
2014-10-26 22:36:33
http://php5.kiev.ua/manual/ru/function.iterator-to-array.html
Автор:
When using iterator_to_array() on an SplObjectStorage object, it's advisable to set $use_keys to false.

The resulting array is identical, since the iterator keys produced by SplObjectStorage::key() are always integers from 0 to (COUNT-1). Passing $use_keys=false cuts out the unnecessary calls to SplObjectStorage::key(), giving a slight performance advantage.
2015-06-08 00:21:28
http://php5.kiev.ua/manual/ru/function.iterator-to-array.html
Generator approach

function scandir_deep($dir)
{
  foreach (scandir($dir) as $key => $value)
    if (in_array($value, [".",".."]))
      continue;
    else if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
      yield $value => scandir_deep($dir . DIRECTORY_SEPARATOR . $value);
    else
      yield $value;
}
2017-01-22 06:26:54
http://php5.kiev.ua/manual/ru/function.iterator-to-array.html
One important thing to remember is that in iterator can be infinite. Not all iterators necessarily end. If iterator_to_array is used on such an iterator, it will exhaust the available memory, and throw a fatal error.

For example, consider the following code:

<?php

function fibonacci(): Generator
{
    yield 
$a 1;
    yield 
$b 2;

   
start:
    yield 
$c $a $b;
   
$a $b;
   
$b $c;
    goto 
start;
}

$fibonacciSequence fibonacci();
iterator_to_array($fibonacciSequence);

?>

Since <?php fibonacci(); ?> generates an infinite fibonacci sequence, which is valid, since it is actually an infinite sequence, then attempting to convert it to an array will fail.
2022-03-21 17:55:58
http://php5.kiev.ua/manual/ru/function.iterator-to-array.html

    Поддержать сайт на родительском проекте КГБ