Foreach Loops

Foreach loops allow you to quickly traverse through an array.

$array = array("name" => "Jet", "occupation" => "Bounty Hunter" ); 
foreach ($array as $val) {
    echo "<P>$val";
}
Prints out:
<P>Jet
<P>Bounty Hunter

You can also use foreach loops to get the key of the values in the array:

foreach ($array as $key => $val) { 
    echo "<P>$key : $val";
}
Prints out:
<P>name : Jet
<P>occupation : Bounty Hunter

Get Advanced PHP for Web Professionals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.