[Previous] [Contents] [Next]


Associative arrays


An associative array uses string indexes-or keys-to access values stored in the array. An associative array can be constructed using array( ), as shown in the following example, which constructs an array of integers:

$array = array("first"=>1, "second"=>2, "third"=>3);

// Echo out the second element: prints "2"
echo $array["second"];

The same array of integers can also be created with the bracket syntax:

$array["first"] = 1;
$array["second"] = 2;
$array["third"] = 3;

There is little difference between using numerical or string indexes to access values. Both can reference elements of an associative array, but this is confusing and should be avoided in practice.

Associatively indexed arrays are particularly useful for interacting with the database tier. Arrays are used extensively in Chapter 4, Chapter 5, and Chapter 6, and more examples and array-specific functions are presented there.


[Previous] [Contents] [Next]