PHP Arrays

A one-dimensional array variable can hold any number of data items of the same type, which are stored in contiguous memory locations. The array has a unique identifier, and individual array elements can be referenced using a combination of the identifier and an index number called an array subscript. Array elements in PHP are indexed from 0, so the first element in an array has the subscript 0, while the last element has a subscript equal to one less than the number of items in the array. An array is a simple structure that makes it much easier to manipulate an ordered list of values, as opposed to having separate variables to hold each item in the list, especially if a large number of items are involved. Arrays are often used together with programming loops (dealt with in the next section) to processes ordered lists of values. PHP has three kinds of array:

The following script demonstrates how numeric arrays can be created and accessed:


<html>
  <body>

<?php

  // Declare and initialise an array simultaneously

  $archPeriod = array("Paleolithic","Neolithic","Calcolithic", "Early Bronze Age", "Middle Bronze Age", "Late Bronze Age", "Iron Age I", "Iron Age II");

  // Declare and initialise individual array elements

  $timeFrame[0] = "2,000,000 BC - 8300 BC";
  $timeFrame[1] = "8,300 BC - 4,500 BC";
  $timeFrame[2] = "4,500 BC – 3,300 BC";
  $timeFrame[3] = "3,300 BC – 2,000 BC";
  $timeFrame[4] = "2,000 BC – 1,550 BC";
  $timeFrame[5] = "1,550 BC – 1,200 BC";
  $timeFrame[6] = "1,200 BC – 1,000 BC";
  $timeFrame[7] = "1,000 BC – 586 BC";

  $pCount = count($archPeriod);

  echo "The earliest pre-historic archaeological period was the " . $archPeriod[0] . " (" . $timeFrame[0] . ").<br><br>";

  echo "The most recent pre-historic archaeological period was the " . $archPeriod[$pCount-1] . " (" . $timeFrame[$pCount-1] . ").<br><br>";

?>

  </body>
</html>


The built-in function count() is used to get the size of the array into a variable ($pCount). Note that the last item in each array is indexed using $pCount–1. This is because the arrays are indexed from 0, so the index for the last item in each array will be the number of items in the array, less one. Enter the code above into a text editor and save it with the filename "arrays_01.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser's address bar:


http://localhost/arrays_01.php


You should see output that is something like the screenshot below.


The output from arrays_01.php

The output from arrays_01.php



Associative arrays

In an associative array, instead of using a numeric index we associate each stored value with a key. A key is essentially the name of a variable within the array, and can be anything you like. The following script is a modified version of the previous example that demonstrates how associative arrays can be created and accessed:


<html>
  <body>

<?php

  // Declare and initialise an associative array

  $archPeriod['Paleolithic'] = "2,000,000 BC - 8300 BC";
  $archPeriod['Neolithic'] = "8,300 BC - 4,500 BC";
  $archPeriod['Calcolithic'] = "4,500 BC – 3,300 BC";
  $archPeriod['Early Bronze Age'] = "3,300 BC – 2,000 BC";
  $archPeriod['Middle Bronze Age'] = "2,000 BC – 1,550 BC";
  $archPeriod['Late Bronze Age'] = "1,550 BC – 1,200 BC";
  $archPeriod['Iron Age I'] = "1,200 BC – 1,000 BC";
  $archPeriod['Iron Age II'] = "1,000 BC – 586 BC";

  $pCount = count($archPeriod);

  echo "The pre-historic archaeological periods are: <br><br>";

  // Use a simple loop to output the key-value pairs (note
  // that multiple statements to be executed on each
  // iteration of a loop are enclosed within curly braces).

  while($element = each($archPeriod))
  {
    echo $element[ 'key' ];
    echo ' - ';
    echo $element[ 'value' ];
    echo '<br>';
  }

?>

  </body>
</html>


Don’t worry too much at this stage exactly how the while() loop construct works. We will look at loops in some detail in another section. For now, it is enough to know that we are accessing each key-value pair in turn, and displaying them on the screen. Enter the code above into a text editor and save it with the filename "arrays_02.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser's address bar:


http://localhost/arrays_02.php


You should see output that is something like the screenshot below.


The output from arrays_02.php

The output from arrays_02.php