In previous lessons we discussed creating the arrays and accessing the elements using keys and indexes. There is always a need to iterate through each element of the array. You have already learned about looping constructs in PHP that can be used to iterate through array elements. Here in this lesson you will understand how to iterate PHP arrays with each() and foreach().
These two consider the arrays as collections i.e. a set of multiple elements of same type. The best feature of these is that you don’t need to bother about the loop counters. each() and foreach() automatically loop through the collection of elements. The current element is saved in a variable or key-value pair of variables which can be used for any calculation or simply printing on screen with echo statement.
each() loop for PHP Array Iteration
The syntax of each() is as follows
While (($key,$value)=each($array-name)){
Statements to be executed in each iteration
}
Here the while loop is used to iterate. each() function will take $array-name as parameter. As long as the elements are available in array, the key or index and value pair is assigned to the ($key,$value) pair. This key and value pair can be processed in each iteration. When the array elements are no longer available (all elements have been iterated) while loop terminates passing control to next statement after the block.
Look at this code example
<?php
$arrVals=array(30,23,45,761,239);
echo "<h3>Before iteration with each</h3><br>";
while ( list($k, $v) = each($arrVals))
{
echo $k."-->".$v."<br>";
}
echo "<h3>After iteration with each</h3><br>";
?>
Output:
You can see that we have used $k and $v variables for key and value pairs. The output displays the respective key and values of each element in the array $arrValues. Read here for further details
foreach() loop for PHP Array Iteration
The syntax of foreach() looing construct is as follows
foreach ($array-name as $value|$key=>$value)
{
Statements to be executed in each iteration
}
Here the foreach loop is used to iterate. foreach() will take $array-name as first parameter. As long as the elements are available in array, the value or index and value pair is assigned to the $value or ($key,$value) pair, respectively. This key and value pair can be processed in each iteration. When the array elements are no longer available (all elements have been iterated) foreach loop terminates passing control to next statement after the block.
Look at this code example
<?php
$arrVals=array(30,23,45,761,239);
echo "<h3>Before iteration with foreach with only value </h3>";
foreach ( $arrVals as $v)
{
echo $v."<br>";
}
echo "<h3>After iteration with foreach with key value pair</h3><br>";
foreach ( $arrVals as $k=>$v)
{
echo $k."-->".$v."<br>";
}
echo "<h3>After iteration with foreach</h3><br>";
?>
Output:
You can see that foreach loop executes with only value or key and value pairs. Read the official PHP documentation for details
Task : Create an array of weekdays. Using each() and foreach() display the elements of this array .