PHP Arrays

An array is a type of variable that can hold multiple values within it. This can be useful where you want to store a number of associated items. For example, if you had a list of cities, say Bath, Birmingham, Cambridge, Gloucester and York, without an array they would need to be stored in separate variables as follows.

$city1 = "Bath";
$city2 = "Birmingham";
$city3 = "Cambridge";
$city4 = "Gloucester";
$city5 = "York";

There are a number of disadvantages to storing the cities in this way. Firstly, if you wanted to add another city to the list then you would have to declare another variable. Secondly, if you wanted to do something programmatically with each city, such as add which country they are in, you would have to reference each variable.

$city1 .= ", England"; // city1 now contains "Bath, England"
$city2 .= ", England"; // city2 now contains "Birmingham, England"
$city3 .= ", England"; // city3 now contains "Cambridge, England"
$city4 .= ", England"; // city4 now contains "Gloucester, England"
$city5 .= ", England"; // city5 now contains "York, England"

This process can be simplified by the use of an array because all of the cities can be stored in one place and to programmatically alter each city, a loop, called the 'foreach' loop, which is a variation on the 'for' loop, can be used. Adding an extra city would be as easy as adding an extra value to the array. It is also easier to output the values in the array to the screen.

// Declare the array and populate it with the list of cities.
$cities = array("Bath","Birmingham","Cambridge","Gloucester","York");

// Loop through the array to add the country where the cities are located.
foreach ($cities as &$value) {
   $value.= ", England";
   // Display the city.
   echo "$value<br>";
}

The above will produce the following output.

Bath, England
Birmingham, England
Cambridge, England
Gloucester, England
York, England

Values in an array can also be referenced by their numbered position, or key. You will notice in the following example that the first position is referred to as '0' and not '1'.

// Declare the array and populate it with the list of cities.
$cities = array("Bath","Birmingham","Cambridge","Gloucester","York");

// Display the array values in a sentence.
echo $cities[0] . ", " . $cities[1] . ", " . $cities[2] . ", " . 
     $cities[3] . " and " . $cities[4] . " are cities in England.";

The result of the above is shown below.

Bath, Birmingham, Cambridge, Gloucester and York are cities in England.

Multidimensional Arrays

Multidimensional arrays are useful where you have more than one piece of information, so, to continue with the above example, instead of just storing the name of the city in the array, the country it is in can also be stored.

// Declare the array and populate it with the list of cities.
$cities = array
   (
      array("Bath","England"),
      array("Cardiff","Wales"),
      array("Miami","United States"),
      array("Paris","France"),
      array("Sydney","Australia")
   );

In order to output this information, nested 'foreach' loops need to be used. The numbered position, or key, in the array is used to determine what text to output.

// Loop through the array and display the information.
foreach ($cities as $value1) {
   foreach ($value1 as $key => $value2) 
   {
      if ($key == 0)
      {
         echo "The city of $value2 is in the country ";
      }
      else
      {
         echo "$value2.<br>";
      }
   }
}

The output from the above will look like this.

The city of Bath is in the country England.
The city of Cardiff is in the country Wales.
The city of Miami is in the country United States.
The city of Paris is in the country France.
The city of Sydney is in the country Australia.

Instead of having a numerical key to reference the position in the array, more explanatory text can be assigned. This means that instead of '0' referring to the position of city in the array, the text 'city' can be assigned as the key. Similarly, the '1' for country can be replaced with the text 'country'. The above example can be re-written as follows.

// Declare the array and populate it with the list of cities.
$cities = array
   (
      array(city => "Bath", country => "England"),
      array(city => "Cardiff", country => "Wales"),
      array(city => "Miami", country => "United States"),
      array(city => "Paris", country => "France"),
      array(city => "Sydney", country => "Australia")
   );

// Loop through the array and display the information.
foreach ($cities as $value1) {
   foreach ($value1 as $key => $value2) 
   {
      if ($key == "city")
      {
         echo "The city of $value2 is in the country ";
      }
      else
      {
         echo "$value2.<br>";
      }
   }
}