PHP Foreach Loop

PHP Foreach Loop

The creators of PHP have gone to great lengths to make the language easy to use. So, not content with the loop structures already provided, they added another one especially for arrays: the foreach … as a loop. Using it, you can step through all the items in an array, one at a time, and do something with them.
The process starts with the first item and ends with the last one, so you don’t even have to know how many items there are in an array.

Example 6-6 shows how each … as can be used to rewrite Example 6-3.
Example 6-6. Walking through a numeric array using foreach … as

<?php
$paper = array("Copier", "Inkjet", "Laser", "Photo");
$j = 0;
foreach($paper as $item)
{
echo "$j: $item<br>";
++$j;
}
?>

When PHP encounters a foreach statement, it takes the first item of the array and places it in the variable following the as a keyword; and each time control flow returns to the foreach, the next array element is placed in the as a keyword. In this case, the variable $item is set to each of the four values in turn in the array $paper. Once all values have been used, execution of the loop ends. The output from this code is exactly the same as

Example 6-3. Now let’s see how foreach works with an associative array by taking a look at
Example 6-7, which is a rewrite of the second half of Example 6-5.
Example 6-7. Walking through an associative array using foreach … as

<?php
$paper = array(

'copier' => "Copier & Multipurpose",
'inkjet' => "Inkjet Printer",
'laser' => "Laser Printer",
'photo' => "Photographic Paper"

);

foreach($paper as $item => $description)
echo "$item: $description<br>";
?>

Remember that associative arrays do not require numeric indexes, so the variable $j is not used in this example. Instead, each item of the array $paper is fed into the key/value pair of variables $item and $description, from which they are printed out. The displayed result of this code is as follows:

copier: Copier & Multipurpose
inkjet: Inkjet Printer
laser: Laser Printer
photo: Photographic Paper

As an alternative syntax to foreach … as you can use the list function in conjunction with each function, as in Example 6-8.

Example 6-8. Walking through an associative array using each and list

<?php
$paper = array('copier' => "Copier & Multipurpose",
'inkjet' => "Inkjet Printer",
'laser' => "Laser Printer",
'photo' => "Photographic Paper");
while (list($item, $description) = each($paper))
echo "$item: $description<br>";
?>

In this example, a while loop is set up and will continue looping until each function returns a value of FALSE. Each function acts like foreach: it returns an array containing a key/value pair from the array $paper and then moves its built-in pointer to the next pair in that array. When there are no more pairs to return, each returns FALSE. The list function takes an array as its argument (in this case, the key/value pair returned by the function each) and then assigns the values of the array to the variables listed within parentheses. You can see how list works a little more clearly in Example 6-9, where an array is created out of the two strings Alice and Bob and then passed to the list function, which assigns those strings as values to the variables $a and $b.

Example 6-9. Using the list function

<?php
list($a, $b) = array('Alice', 'Bob');
echo "a=$a b=$b";
?>

The output from this code is:

a=Alice b=Bob

 

So you can take your pick when walking through arrays. Use foreach … as to create
a loop that extracts values to the variable following the as, or use each function and
create your own looping system.

<?php 
$colors = array("red", "green", "blue", "yellow"); 

foreach ($colors as $value) {
    echo "$value <br>";
}
?>

d<< PrevNext >>

Loading