PHP Manual
Prev | Chapter 6. Types | Next

# Arrays

Arrays actually act like both hash tables (associative arrays) and indexed arrays (vectors).

## Single Dimension Arrays

PHP supports both scalar and associative arrays. In fact, there is no difference between the two. You can create an array using the list() or array() functions, or you can explicitly set each array element value.

      1
      2 $a[0] = "abc";
      3 $a[1] = "def";
      4 $b["foo"] = 13;
      5

You can also create an array by simply adding values to the array. When you assign a value to an array variable using empty brackets, the value will be added onto the end of the array.

      1
      2 $a[] = "hello"; // $a[2] == "hello"
      3 $a[] = "world"; // $a[3] == "world"
      4

Arrays may be sorted using the asort(), arsort(), ksort(), rsort(), sort(), uasort(), usort(), and uksort() functions depending on the type of sort you want.

You can count the number of items in an array using the count() function.

You can traverse an array using next() and prev() functions. Another common way to traverse an array is to use the each() function.

## Multi-Dimensional Arrays

Multi-dimensional arrays are actually pretty simple. For each dimension of the array, you add another [key] value to the end:

      1
      2 $a[1]      = $f;               # one dimensional examples
      3 $a["foo"]  = $f;
      4
      5 $a[1][0]     = $f;             # two dimensional
      6 $a["foo"][2] = $f;             # (you can mix numeric and associative indices)
      7 $a[3]["bar"] = $f;             # (you can mix numeric and associative indices)
      8
      9 $a["foo"][4]["bar"][0] = $f;   # four dimensional!
     10

In PHP3 it is not possible to reference multidimensional arrays directly within strings. For instance, the following will not have the desired result:

      1
      2 $a[3]['bar'] = 'Bob';
      3 echo "This won't work: $a[3][bar]";
      4

In PHP3, the above will output `This won't work: Array[bar]`. The string concatenation operator, however, can be used to overcome this:

      1
      2 $a[3]['bar'] = 'Bob';
      3 echo "This will work: " . $a[3][bar];
      4

In PHP4, however, the whole problem may be circumvented by enclosing the array reference (inside the string) in curly braces:

      1
      2 $a[3]['bar'] = 'Bob';
      3 echo "This will work: {$a[3][bar]}";
      4

You can "fill up" multi-dimensional arrays in many ways, but the trickiest one to understand is how to use the array() command for associative arrays. These two snippets of code fill up the one-dimensional array in the same way:

      1
      2 # Example 1:
      3
      4 $a["color"]	= "red";
      5 $a["taste"]	= "sweet";
      6 $a["shape"]	= "round";
      7 $a["name"]	= "apple";
      8 $a[3]		= 4;
      9
     10 # Example 2:
     11 $a = array(
     12      "color" => "red",
     13      "taste" => "sweet",
     14      "shape" => "round",
     15      "name"  => "apple",
     16      3       => 4
     17 );
     18

The array() function can be nested for multi-dimensional arrays:

      1
      2 <?
      3 $a = array(
      4      "apple"  => array(
      5           "color"  => "red",
      6           "taste"  => "sweet",
      7           "shape"  => "round"
      8      ),
      9      "orange"  => array(
     10           "color"  => "orange",
     11           "taste"  => "tart",
     12           "shape"  => "round"
     13      ),
     14      "banana"  => array(
     15           "color"  => "yellow",
     16           "taste"  => "paste-y",
     17           "shape"  => "banana-shaped"
     18      )
     19 );
     20
     21 echo $a["apple"]["taste"];    # will output "sweet"
     22 ?>
     23

Prev | Home | Next
Strings | Up | Objects