|Classes of Carlos Artur Curvelo da Matos||>||Foreacher PHP Array Iterator classes||>||README.md||>||Download|
Dealing with arrays and data can be consuming in terms of resources and sometimes troublesome. PHP natively offer a serie of classes that, from converting arrays in objects, allow us to perform a number of advanced queries and loops on data. This library aims to simplify the process of using classes like ArrayIterator, InfiniteIterator, RegexIterator and so on, providing a comprehensive and friendly way to use them and apply functions and methods to their results.
The class Iterator is the heart of this library. It stores all arrays under keys, which can be lately used to perform operations and loops with their data. The remaining classes extend those from PHP, in order to support the Iterator class methods. So, first and foremost, you can start by providing some arrays and creating a new instance of Iterator.
Once the arrays are stored, you can start performing operations with them individually, without changing or modifying the initial data, which remains protected under the property $arrays. Also, the class has an additional method push($id, $data) that can be used anytime to add new named arrays to the pool.
First utility of this class involves the possibility of limiting the loops to part of the named array. That means you can loop in an array starting from the third value, for instance. Also, you can do the opposite: just loop over the last three elements. Finally, one can also loop just starting from a given element.
What about the operations to be applied to each of the looped elements. Well, of course you can provide a callable function when determining the limits of your loop. So let's see an example:
Performing a loop (n) times
Another possibility is using the method looping() on any of the stored arrays to perform a partial ou (n) sequential loops over the same array elements. For this case, the second argument actually accepts not only integers, but floats as well. So:
Looping all stored arrays
It is also possible to loop all stored arrays applying the same function. In this case, the result will be a single array in which elements are actually subarrays that bring a key-value logic, where the key refers to the stored array name and the value the respective value in the loop position. Let's apply it to the same two arrays to make it clearer.
It is also possible to apply filters before starting the loop, by using the filterValues() method. Doing that, the iteration won't be occurring for the filtered values. The method accepts integers, strings, floats or an array of those elements.
From 1.2.0v, stored arrays can be traversed only on matching elements, from a given Regex expression, using the method regexMatch().