Foreach and Arrays

The foreach syntax

Often one has an array of data and there is a need to loop through all the values. For instance, when once has stored information in an array after retrieving them from the database. An ideal way to loop through this data is to use the looping command: Foreach. This does exactly what you expect; For each element of the input array, some command specified ater, is defined. As foreach is a function, everything it has to work on is placed in curly brackets.

The basics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
 
// Lets first define an array
$MyTestArray = array(
  1 => "One Duck",
  2 => "Two Duck",
  3 => "Three Duck" );
 
// Echo echo value in the array and separate with linebreaks:
 
foreach($MyTestArray as $key => $value)
  {
  echo "Element: ". $key .", with value: " . $value . "<br />";
  }
 
?>
<?php

// Lets first define an array
$MyTestArray = array(
  1 => "One Duck",
  2 => "Two Duck",
  3 => "Three Duck" );

// Echo echo value in the array and separate with linebreaks:

foreach($MyTestArray as $key => $value)
  {
  echo "Element: ". $key .", with value: " . $value . "<br />";
  }

?>

This will create the folowing output:

Element: 1, with value One Duck <br />
Element: 2, with value Two Duck <br />
Element: 3, with value Three Duck <br />

You may have noticed that I specified a whole number of variables in the foreach loop. Let us take a closer loop to what we did here:
foreach(MyTestArray as $key => $value)

When we translate that we effectively say: For each value in the array, place the key in variable $key, and place the actual value in the variable $value. Then proceed to whatever is between the brackets. If you do not need the key, you could leav out the $key specification, and just use foreach(MyTestArray as $value). Note: The variables $key and $value can be anything you want; You could also use foreach($MyTestArray as $a => $b).

Expanding

So.. If we do not need to define the $key component of the command, why is this option available? Well, first of all, you may actually have stored some of the information you would like to use in the key. A very logical thing to store would be the record ID in your database. So if you create a series of links, you could for instance do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
// Lets first define an array
$MyTestArray = array(
  1 => "One Duck",
  2 => "Two Duck",
  3 => "Three Duck" );
 
// Echo echo value in the array and separate with linebreaks:
 
foreach($MyTestArray as $key => $value)
  {
  echo "<a href=\"index.php?set=".$key."\">" . $value . "</a><br />";
  }
?>
<?php

// Lets first define an array
$MyTestArray = array(
  1 => "One Duck",
  2 => "Two Duck",
  3 => "Three Duck" );

// Echo echo value in the array and separate with linebreaks:

foreach($MyTestArray as $key => $value)
  {
  echo "<a href=\"index.php?set=".$key."\">" . $value . "</a><br />";
  }
?>

Indexing other arrays

Instead of using just one array, one could of course also use the $key variable to index another array..

Leave a Reply