(PHP 5 >= 5.1.0, PHP 7, PHP 8)
iterator_to_array — イテレータを配列にコピーする
イテレータの要素を配列にコピーします。
iteratorコピーしたいイテレータ。
preserve_keysイテレータの要素のキーをインデックスとして使用するかどうか。
 キーが array  あるいは object 
 の場合は、警告が発生します。
 キーが null  の場合は空文字列に変換し、キーが float 
 の場合は int  型になるよう切り詰めます。
 キーが resource  の場合は警告を発し、リソース ID に変換します。
 また、キーが bool  の場合は整数値に変換します。
 
注意:
このパラメータを省略したり
trueを指定したりした場合は、 重複するキーは上書きされます。そのキーに対して最後にあらわれた値が array で返されることになります。 このパラメータをfalseにすると、重複があってもすべての値を返します。
 iterator の要素を含む配列を返します。
 
| バージョン | 説明 | 
|---|---|
| 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"
}
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.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"
)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 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.