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