Hướng dẫn php iterator to array

[PHP 5 >= 5.1.0, PHP 7, PHP 8]

iterator_to_arrayCopy the iterator into an array

Description

iterator_to_array[Traversable $iterator, bool $preserve_keys = true]: array

Parameters

iterator

The iterator being copied.

preserve_keys

Whether to use the iterator element keys as index.

If a key is an array or object, a warning will be generated. null keys will be converted to an empty string, float keys will be truncated to their int counterpart, resource keys will generate a warning and be converted to their resource ID, and bool keys will be converted to integers.

Note:

If this parameter is not set or set to true, duplicate keys will be overwritten. The last value with a given key will be in the returned array. Set this parameter to false to get all the values in any case.

Return Values

An array containing the elements of the iterator.

Examples

Example #1 iterator_to_array[] example

The above example will output:

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"
}

jerome at yazo dot net

13 years ago

Using the boolean param :



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"
]



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"
]

joksnet at gmail dot com

7 years ago

To generate an deep array from nested iterators:



I use it to test an iterator: //gist.github.com/jm42/cb328106f393eeb28751

Harry Willis

7 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.

wizzard351 at yahoo dot com

5 months 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:



Since 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.

chad 0x40 herballure 0x2e com

14 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.

enelar at develop-project dot ru

5 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;
}

Chủ Đề