Open In App

PHP iterator_to_array() Function

The iterator_to_array() function is an inbuilt function in PHP that is used to copy the iterator object (e.g. objects implementing the Iterator or IteratorAggregate interface) into an array. An iterator is an object that allows you to loop through a set of values one at a time without knowing the underlying data structure.

Syntax:

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

Parameters: This function accepts two parameters that are described below.

Return Values: The iterator_to_array() function returns the array with all containing values of iterator.

Program 1: The following program demonstrates the iterator_to_array() function.

<?php

class SimpleIterator implements Iterator {
    private $position = 0;
    
    private $data = array('apple', 
        'banana', 'cherry', 'date');

    public function rewind() {
        $this->position = 0;
    }

    public function current() {
        return $this->data[$this->position];
    }

    public function key() {
        return $this->position;
    }

    public function next() {
        ++$this->position;
    }

    public function valid() {
        return isset($this->data[$this->position]);
    }
}

$iterator = new SimpleIterator();
$array = iterator_to_array($iterator);

print_r($array);

?>

Output
Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)

Program 2: The following program demonstrates the iterator_to_array() function.

<?php
    
// Create an Array
$data = [100, 200, 300, 400];

// Create an ArrayIterator with array data
$iterator = new ArrayIterator($data);

// Convert the iterator to an array
// without preserving keys
$array = iterator_to_array($iterator, false);

// Print the resulting array
print_r($array);

?>


Output

Array
(
    [0] => 100
    [1] => 200
    [2] => 300
    [3] => 400
)

Program 3: The PHP code defines a SimpleIterator class implementing the Iterator interface. It iterates over an array of numbers and converts the iterator to an array using iterator_to_array(), then prints it.

<?php
  // Define a simple iterator
class SimpleIterator implements Iterator {
    private $position = 0;
    private $array = array('1', '2', '3','4','5','6','7','8','9','10','11','12','13','14');

    public function __construct() {
        $this->position = 0;
    }

    public function rewind() {
        $this->position = 0;
    }

    public function current() {
        return $this->array[$this->position];
    }

    public function key() {
        return $this->position;
    }

    public function next() {
        ++$this->position;
    }

    public function valid() {
        return isset($this->array[$this->position]);
    }
}

// Create an instance of the iterator
$iterator = new SimpleIterator();

// Convert the iterator to an array
$array = iterator_to_array($iterator);

// Print the resulting array
print_r($array);

?>


Output:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => 12
[12] => 13
[13] => 14
)

Reference: https://www.php.net/manual/en/function.iterator-to-array.php

Article Tags :