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
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 foreach
can be used to rewrite Example 6-3.
<?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
keyword, and
each time control flow returns to the foreach
the next array element is placed in the
as
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 for 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.
<?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 where they are printed out.
The 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 the each
function, as in Example 6-8.
<?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 the each
function returns a value of FALSE
. The 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 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
.
<?php list($a, $b) = array('Alice', 'Bob'); echo "a=$a b=$b"; ?>
The output from this code is:
a=Alice b=Bob
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 the each
function and create your own looping
system.