The Foreach Loop

The code

foreach ($array as $someVar) {
  echo ($someVar . "<br />");


foreach ($array as $key => $someVar) {
  echo ($key." holds the value ".$someVar."<br />");


The foreach loop is a special form of the standard for loop. The example above will print all the values of $array. The foreach structure is a convenient way to loop through an array.

Simple foreach statement

Foreach loops are useful when dealing with an array indexed with arbitrary keys (e.g. non-numeric ones):

$array = array(
  "1st" => "My House",
  "2nd" => "My Car",
  "3rd" => "My Lab"

To use the classical for structure, you'd have to write:

// get all the array keys
$arrayKeys = array_keys($array);
// get amount of values from array
$count = count($array);
// loop through the keys
for ($i = 0; $i < $count; $i++) {
  // get each array value using its key
  echo $array[($arrayKeys[$i])] . "<br />";

Basically, an array value can be accessed only from its key: to make sure you get all the values, you first have to make a list of all the existings keys, then grab all the corresponding values. The access to first array value, the previous example does the following steps:

$firstKey = $arrayKeys[0];       // which is '1st'
$firstValue = $array[$firstKey]; // which is 'My House' ($array('1st'))

The foreach structure does all the groundwork for you:

foreach ($array as $someVar) {
  echo $someVar . "<br />";

Note how the latter example is easier to read (and write). Both will output:

My House
My Car
My Lab

foreach with key values

If you need to use the array's keys in the body of your loop, just add the variable as in the following statement. The phrase '$myKey => $value' makes the value of the key accessible:

foreach ($array as $myKey => $value) {
  // use $myKey

Note that this is very useful when constructing a dropdown list in HTML. You can use a foreach-loop to have the $myKey variable inserted into the value="..." part and the $value as the actual text.

This form mimics the way we used custom keys for $array elements. It will not only assign the elements of $array to $someVar, but also assign the keys of those elements to $i.

Crystal Clear app terminal.png PHP Code:

$array = array("1st" => "My House", "2nd" => "My Car", "3rd" => "My Lab");
foreach ($array as $key => $someVar) {
  echo $key . ": " . $someVar . "<br />\n";

Crystal Clear app kscreensaver.png PHP Output:

 1st: My House<br />
 2nd: My Car<br />
 3rd: My Lab<br />

Crystal 128 yast default.png HTML Render:

 1st: My House
 2nd: My Car
 3rd: My Lab

Note that, if you change the assigned variable inside the foreach loop, the change will not be reflected to the array. Therefore, if you need to change elements of the array you need to change them by using the array key. Example:

$array = array(
  "1st" => "My House",
  "2nd" => "My Car",
  "3rd" => "My Lab"

foreach ($array as $i => $someVar) {
  // OK
  if($someVar == 'My Lab') {
    $array[$i] = 'My Laboratory';

  // doesn't update the array
  $someVar = 'Foo';

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at Create an account using LinkedIn to manage and organize your omni-channel knowledge. is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us