PHP developers sometimes need to turn any Traversable iterator into an array. This comes up when preparing data, displaying results, or integrating information with other services.
The iterator_to_array() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.
What Is iterator_to_array() in PHP?
Turn any Traversable iterator into an array.
Its syntax is:
iterator_to_array(Traversable|array $iterator, bool $preserve_keys = true): array
The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.
Important: Preserving keys can overwrite earlier values when an iterator yields duplicate keys; consuming a generator exhausts it.
Parameters:
$iterator— A named input or configuration argument; use the declared type and optional default shown above.$preserve_keys— A named input or configuration argument; use the declared type and optional default shown above.
Example 1: Basic Usage
This first example uses a small input to demonstrate the main result of iterator_to_array().
<?php
$iterator = new ArrayIterator(["red", "green", "blue"]); print_r(iterator_to_array($iterator));
Output:
Array
(
[0] => red
[1] => green
[2] => blue
)
The output demonstrates the primary operation: turn any Traversable iterator into an array.
Example 2: Understanding Another Case
This second example changes the input or option to reveal a different behavior of iterator_to_array().
<?php
$iterator = new ArrayIterator([2 => "A", 5 => "B"]); print_r(iterator_to_array($iterator, false));
Output:
Array
(
[0] => A
[1] => B
)
Compared with the first example, this one illustrates the same operation with a different input shape or option. Preserving keys can overwrite earlier values when an iterator yields duplicate keys; consuming a generator exhausts it.
Practical Example: A Real Application Scenario
Here is a practical use of iterator_to_array() with realistic sample information. Adapt the values to match your application.
<?php
$entries = (function () { yield "first" => 10; yield "second" => 20; })(); print_r(iterator_to_array($entries));
Output:
Array
(
[first] => 10
[second] => 20
)
This is a useful starting point, but production code should check errors and validate values before performing the operation. Preserving keys can overwrite earlier values when an iterator yields duplicate keys; consuming a generator exhausts it.
Important Behavior and Edge Cases
Preserving keys can overwrite earlier values when an iterator yields duplicate keys; consuming a generator exhausts it.
- Input: Use the expected PHP types and validate user-provided values.
- Result: Check the return value separately from any modifications to the input.
- Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
iterator_to_array() | Turn any Traversable iterator into an array. |
| Related functions | iterator_to_array preserves keys by default while iterator_count consumes an iterator. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming iterator_to_array() behaves like every similar function | Preserving keys can overwrite earlier values when an iterator yields duplicate keys; consuming a generator exhausts it. |
| Choosing a related operation without checking differences | iterator_to_array preserves keys by default while iterator_count consumes an iterator. |
| Ignoring prerequisites or return values | Verify extensions and data sources, and handle unexpected results explicitly. |
Key Takeaways
iterator_to_array()lets you turn any Traversable iterator into an array.- Preserving keys can overwrite earlier values when an iterator yields duplicate keys; consuming a generator exhausts it.
- iterator_to_array preserves keys by default while iterator_count consumes an iterator.
- Check input, return values, and environmental requirements when adapting the examples.
iterator_to_array() provides a focused PHP operation for turn any Traversable iterator into an array. Start with the simple examples, then adapt the practical example with the correct checks for your project.