Assigning arrays within a script

Assigning arrays within a script

Arrays are variables that contain multiple values. A simple array might store the months of the year. To assign this array, you could use the following:

$months = array(“January”, “February”, “March”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”);

This array has 12 elements, and you can address them by their ordinal placement in the array, starting with 0. So the command echo $months[0] would print January and echo $months[11] would print December. To print out all of the val- ues within an array, you could get the length of the array and then set up a loop.

for ($month_number=0; $i<count($months); $i++) {

echo $months[$month_number] . “<br>\n” ; }

The for loop is explained in Chapter 5.

XREF

You can also assign values to arrays with a simple assignment operator. The fol- lowing would work:

$dogs = array(); $dogs[0] = “shepherd”; $dogs[1] = “poodle”;

If you don’t specify the numeral, the value will be tacked on the end of the array. The following line would assign “retriever” to $dogs[2] .

$dogs[] = “retriever”;

Chapter 4: Getting Started with PHP —Variables

There are a variet y of functions that work with arrays (over 40 in PHP 4). Many of these will be covered in Chapter 6.

XREF

Like many programming languages, PHP makes use of associative arrays. If you are new to the concept, elements in associative arrays have “keys” that reference individual elements. This is particularly important when you’re dealing with data- bases. When you fetch rows from your database query you will usually refer to the elements by their keys.

You can assign an associative array in this manner. Here, first_name , last_name , and e-mail are the keys.

$person = array ( “first_name” => “Jay”, “last_name” => “Greenspan”, “e-mail” => “jgreen_1@yahoo.com”

); If you wanted to add to this array, you could assign another value. Notice that

the next line would add an integer into the array, so this array would contain four values, three strings and one integer.

$person[“age”] = 32; Typically, if you want to access both the keys and the values in an associative

array, you would use list()=each() , as in the following code. while (list($key, $value) = each($person))

{ echo “<b>key :</b> $key, value = $value <br>\n”; }

Chapter 5 describes the list()=each() in more detail. Basically, each() pulls the key and value of a single array element; list() takes those values and assigns them to $key and $value, respectively. This process continues until each element in the array has been accessed. If you want to go through the array a second time, you will need to reset the array pointer with reset($person) .

If you wanted to get only the value without the key, or if you were using a non- associative array and wanted to use the list()=each() structure, you would have to do this:

while (list( , $value) = each($person)) {

76 Part II: Working with PHP

echo “value = $value <br>\n”; }

Or, if you want to get at just the keys, you could do this. while (list($key) = each($person))

{ echo “key = $key <br>\n”; }

N OTE

Think about PHP arrays this way: all arrays are associative. A couple of pages back you saw you can assign a basic array without specifying associative keys.For example $myarray= array (“pug”, “poodle”).When this is done, PHP assigns $myarray consecutive numeric keys starting at zero. They behave just like associative keys.You step through them using list() =each(). They make use of the same array functions, many of which are explained in Chapter 6.