Back to DFS's Workshop Page
Back to Agenda Page


Passing an Array

Variables of the same sort, e.g., temperatures, students grades, x-coordinates, are typically placed in arrays to make processing easier.

Here we will use a PHP script to populate an array with a random number of values which we will then make passable by including them as parameters in a link.

In the receiving script, we will check to make sure that the array is not null, and then we will print the contents of the array, showing the subscripts.

Populate the Array

First we will populate an array called A. The number of elements will be from zero to five. The values will range from one to six, as those for a six-sided die. Then we print the array as a comma-separated list enclosed by curly brackets.

$numa = rand(0,5);
for ($i = 1; $i <= $numa; $i++)
  $a[] = rand(1,6);
echo "<p>There are ", count($a), " items in array A.\n";
// Print array A
echo "{";
if (count($a)) {
  $i = 1;
  foreach($a as $aa) {
    echo "$aa";
    if ($i < count($a) ) echo ", ";
    $i++;
  }
}
echo "}\n";

Note the use of $a[] without any subscript in the for loop. The values are placed in the array starting from the zeroth position.

Assuming that the values 4, 5, 3, and 6 have been generated, the following line would be the output.

There are 4 items in array A. {4, 5, 3, 6}

Creating the Link

To send the elements in the array to another script where they can be recognized as elements of an array, we need to have a way of specifying that the values actually belong to an array. This could be done by writing them to a file in the first script and reading them from the file in the second script. However, it can be done without the use of a file by specifying the elements as parameters in a link. This is accomplished by using square brackets, but no subscripts.

// Prepare base of string for link
$sendstr = "<a href=\"arrayreceive.php?";
// Assemble string of parameters for passing A
$arrayastr = "";
if (count($a)) {
  $arrayastr .= "a[]=" . $a[0];
  for ($i = 1; $i < count($a); $i++)
    $arrayastr .= "&a[]=" . $a[$i];
}
// Create link for A if populated
if ($arrayastr) {
  echo "<p>" . $sendstr . $arrayastr . "\">Click to send array A.</a>";
}

This code generates the following HTML link.

<a href="arrayreceive.php?a[]=4&a[]=5&a[]=3&a[]=6>Click to send array A.</a>

Receiving the Array

In using the $_REQUEST associative array, you can copy the whole array from $_REQUEST['a'] to a local variable with the following statement:

$a = $_REQUEST['a'];

When receiving the array, it is important to check to make sure that values for it have actually been passed. In the code below, the if statement

if (!empty($a))

is used to ensure that the array has at least one element. Alternatively, the if statement

if (count($a))

could be used.

echo "<p>There were ", count($a), " items passed in array A.\n";
// Print array A if populated
if (!empty($a)) {
  $i = 0;
  foreach($a as $aa) {
    echo "<p>a[" . $i++ . "] = $aa\n";
  }
}

For the four values passed by the code discussed above, the following output would be produced.

There were 4 items passed in array A.

a[0] = 4

a[1] = 5

a[2] = 3

a[3] = 6 

To see how this code would work, take this link.


© 2003-2005 DFStermole
Created: 4 July 03
Last Modified: 6 Dec 05