Intro
The loop class is useful when trying to keep track of your place within a loop. This class was designed to be used alongside the Field::iterable() method.
The class consists of a contructor and a single method which opens up direct access to the various properties.
Namespace
| Arcadia\Helper |
Properties
| $first | boolean | Whether current iteration is the first |
| $last | boolean | Whether current iteration is the last |
| $index | interger | Current iteration using a zero based index |
| $count | interger | Total iterations |
__construct()
Sets up beginning of loop
Parameters
| $count | required | Number of items in the array |
Examples
Creating a new instance
$data = ['item 1', 'item 2', 'item 3'];
$loop = new \Arcadia\Helper\Loop(count($data));
// first = true, last = false, index = 0; count = 3
iterate()
Progress the internal pointer
Parameters
| None |
Examples
Moving the pointer
$data = ['item 1', 'item 2', 'item 3'];
$loop = new \Arcadia\Helper\Loop(count($data));
// first = true, last = false, index = 0; count = 3
foreach ($data as $item) {
$loop->iterate();
}
// first = false, last = true, index = 2; count = 3