Tutorial by Examples

Sometimes two arrays of the same length need to be iterated together, for example: $people = ['Tim', 'Tony', 'Turanga']; $foods = ['chicken', 'beef', 'slurm']; array_map is the simplest way to accomplish this: array_map(function($person, $food) { return "$person likes $food\n"; ...
This method works by incrementing an integer from 0 to the greatest index in the array. $colors = ['red', 'yellow', 'blue', 'green']; for ($i = 0; $i < count($colors); $i++) { echo 'I am the color ' . $colors[$i] . '<br>'; } This also allows iterating an array in reverse order wi...
Each array instance contains an internal pointer. By manipulating this pointer, different elements of an array can be retrieved from the same call at different times. Using each Each call to each() returns the key and value of the current array element, and increments the internal array pointer. ...
Direct loop foreach ($colors as $color) { echo "I am the color $color<br>"; } Loop with keys $foods = ['healthy' => 'Apples', 'bad' => 'Ice Cream']; foreach ($foods as $key => $food) { echo "Eating $food is $key"; } Loop by reference In the fo...
Php arrayiterator allows you to modify and unset the values while iterating over arrays and objects. Example: $array = ['1' => 'apple', '2' => 'banana', '3' => 'cherry']; $arrayObject = new ArrayObject($array); $iterator = $arrayObject->getIterator(); for($iterator; $iterator-...

Page 1 of 1