PHP 8.4.1 Released!

iterator_to_array

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

iterator_to_array复制迭代器中的元素到数组

说明

iterator_to_array(Traversable|array $iterator, bool $preserve_keys = true): array

复制迭代器中的元素到数组。

参数

iterator

被复制的迭代器。

preserve_keys

是否使用迭代器元素键作为索引。

如果键是 arrayobject,将会生成警告。null 键将会转换为空字符串,float 键将截断为对应的 intresource 键将生成警告并转换为它们的资源 ID,bool 键将转换为整数。

注意:

如果此参数未设置或为 true,则覆盖重复的键。指定键的最后一个值将在返回的 array 中。设置此参数为 false 以获得任何情况下的所有值。

返回值

一个数组,包含迭代器中的元素。

更新日志

版本 说明
8.2.0 iterator 的类型从 Traversable 扩展为 Traversable|array

示例

示例 #1 iterator_to_array() 示例

<?php
$iterator
= new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour'));
var_dump(iterator_to_array($iterator, true));
var_dump(iterator_to_array($iterator, false));
?>

以上示例会输出:

array(4) {
  ["recipe"]=>
  string(8) "pancakes"
  [0]=>
  string(3) "egg"
  [1]=>
  string(4) "milk"
  [2]=>
  string(5) "flour"
}
array(4) {
  [0]=>
  string(8) "pancakes"
  [1]=>
  string(3) "egg"
  [2]=>
  string(4) "milk"
  [3]=>
  string(5) "flour"
}

添加备注

用户贡献的备注 5 notes

up
9
wizzard351 at yahoo dot com
2 years ago
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.
up
6
jerome at yazo dot net
15 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
5
joksnet at gmail dot com
10 years 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
3
Harry Willis
9 years ago
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.
up
-4
enelar at develop-project dot ru
7 years ago
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;
}
To Top