Tuesday 14 August 2018

Traversing Arrays in PHP

PHP provides several ways to traverse arrays using both array iteration functions and language constructs: array_walkarray_maparray_filterforeachlist/each, and for loops. We demonstrate and describe foreach and other looping constructs on this page. We cover the iteration functions on another page.

foreach

PHP's foreach loop provides a convenient way to iterate over arrays. There are two forms: one uses both the key and value of each array entry while the other uses only the value. First we demonstrate the key => value form:
$pets = ['Morie', 'Miki', 'Halo', 'lab' => 'Winnie'];

foreach ($pets as $key => $val) {
    echo "$key => $val \n";
}
/* echo output (page source view)
0 => Morie 
1 => Miki 
2 => Halo 
lab => Winnie 
*/
The body of the loop uses echo to display the key and value of each array element in turn.
In this example, foreach uses only the value of the array elements:
$pets = ['Morie', 'Miki', 'Halo', 'lab' => 'Winnie'];
foreach ($pets as $val) {
    echo "$val \n";
}
/*
Morie 
Miki 
Halo 
Winnie 
*/
The foreach construct can be used to modify values of the array it traverses by using the reference sign (&):
$ar = [1, 2, 3, 4];
// use & (reference sign) to change values in array
foreach ($ar as &$value) {
    $value *= 2;
}

print_r($ar);
/* Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
) */
unset($value); // unset reference to $ar element

list/each

Another common way to traverse arrays uses a while loop with the list language construct and the each function. The following example achieves the same result as the first foreachexample shown above:
$pets = ['Morie', 'Miki', 'Halo', 'lab' => 'Winnie'];

reset($pets); // reset array pointer
while ( list($key, $val) = each($pets) ) {
    echo "$key => $val \n";
}
/*
0 => Morie 
1 => Miki 
2 => Halo 
lab => Winnie 
*/
As you iterate over the array, the each function is used to return the current key-value pair and advance the array pointer. The list function is used to assign the key and value to variables. The while loop ends when the last element in the array is reached. The reset function is invoked to restore the array pointer to the first element in the array.
The list/each construct can be used with both key and value, or just value. The following example produces the same result as the second foreach example shown above:
$pets = ['Morie', 'Miki', 'Halo', 'lab' => 'Winnie'];

reset($pets);
while ( list(, $val) = each($pets) ) {
    echo "$val \n";
}
/*
Morie 
Miki 
Halo 
Winnie
*/

for

for loop can be used to iterate over numerically indexed arrays, as we demonstrate here:
$ar = ['Rudi', 'Morie', 'Halo', 'Miki'];

for ($i=0, $len=count($ar); $i<$len; $i++) {
    echo "$ar[$i] \n";
}
/*
Rudi 
Morie 
Halo 
Miki 
*/
The above example used echo to display the current value in each iteration of the loop. You can also use a for loop to modify the values, as the following demonstrates:
$ar = [1, 2, 3, 4];

for ($i=0, $len=count($ar); $i<$len; $i++) {
    $ar[$i] *= 2;
}
print_r($ar);
/* Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
) */

PHP Array Iteration Functions

Find out more about traversing arrays in PHP using the interation functionsarray_walkarray_map, and array_filter.

0 comments:

Post a Comment