PHP Iterables

Throughout this article, you will learn how PHP iterables can be used to simplify your code and make it easier to read, and how you can use them to simplify your code and make it easier to understand.

There is a data type known as an iterable in PHP which is a type of data that can be iterated over with a foreach loop.

There are a number of arrays and objects that are able to traverse across the Traversable interface.

The concept of PHP iterables provides an easy way of looping through collections of data, getting some information from each item, and executing operations on them.

PHP Iterable – What is it?

Php Iterables are any values that can be passed into a foreach() loop and looped through one after another.

There is a pseudo-type for iterables introduced in PHP 7.1, which can be used as a type of argument for functions and as a return type for functions.

Use Iterables

An iterable keyword can be used in a number of different ways, such as using it as a data type for a function argument or as the mode of return for a function.

The following arguments can be used with an iterable function argument:


<?php function printIterable(iterable $itr) { foreach($itr as $item) { echo $item; } } $array = [1, 2, 3, 4]; printIterable($array); ?>
Return iterable using the iterable data type function:


<?php function getIterable() : iterable { return [1, 2, 3]; } $itr = getIterable(); foreach($itr as $item) { echo $item; } ?>
Note: Method casting is deprecated above PHP 7.1

Create Iterable


An array is an iterable, which means it can be used as an argument to a function that requires an iterable as an argument.


There are many objects that implement the Iterator interface that can be used as arguments to functions that require an iterable as an argument.

Iterators can be used to loop through a list of items which contain a list of methods for looping through them.

There is a pointer that keeps track of one of the elements in the list.

There should be a key associated with each item in the list that can be used to locate that item in the list.

Iterators must have the following methods in order to work:

  • current(): Returns the pointer to the current element, or if there is no pointer, then returns the current element. Any data type is allowed
  • key(): This function returns the key associated with the presently selected element in the list. A number can only be an integer, a float, a boolean or a string.
  • next(): This is a function for moving the pointer to the next item in a list of items
  • rewind(): This method moves the pointer back to the beginning of the list, to the first element
  • valid(): This function should return false if the internal pointer is not pointing to any element (for example, if that is the position at which next() was called at the end of the list), otherwise it should return true.

Implement the Iterator interface as an iterable and use it to iterate over the data:


<?php // Create an Iterator class Number_Iterator implements Iterator { private $data = []; private $counter = 0; public function __construct($data) { // array_values() makes sure that the keys are numbers $this->data = array_values($data);} public function current() { return $this->data[$this->counter]; } public function key() { return $this->counter; }public function next() { $this->counter++; } public function rewind() { $this->counter = 0; } public function valid() { return $this->counter < count($this->data); } } // A function that uses iterables function printIterable(iterable $numberIterator) { foreach($numberIterator as $item) { echo $item; } } $iterator = new Number_Iterator([1,2,3,4,5]); printIterable($iterator); ?>

Example Explanation

In above example we have define a class called Number_Iterator that implements the Iterator interface. This interface requires the implementation of several methods, including current(), key(), next(), rewind(), and valid().

  • The __construct() method of the Number_Iterator class takes an array of numbers and initializes the $data property with its values, making sure the keys are sequential numbers.
  • The current() method returns the current element of the iterator, which is simply the value of the current index in the $data array.
    The key() method returns the current index of the iterator.
  • The next() method increments the current index of the iterator.
  • The rewind() method resets the current index of the iterator to the beginning.
  • The valid() method returns true if the current index is less than the total number of elements in the $data array.
  • After defining the Number_Iterator class, the code defines a function called printIterable().
  • This function takes an iterable as an argument and iterates over it using a foreach loop.
Finally, the code creates an instance of the Number_Iterator class with an array of numbers and passes it as an argument to the printIterable() function. The function then iterates over the numbers and prints them on the screen.
We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *