PHP Array Primer


Crash Course For PHP Arrays

These various examples should shed some light about PHP arrays. The examples show simple arrays, indexed arrays and multidimensional arrays. 

Simple Array 

This simple arrays contains
2 items. Each item has a key and a value. The first element in the array has the key 'first' and a value 'myfirst' while the second item has a key 'second' and value 'mysecond'. All keys in an array have a name or a numerical value. In this example, the key is a name.

When you want to output an item in an array, you simply use the array name followed by the key that is enclosed in brackets.

$mypages = array(
'first' => 'myfirst',
'second' => 'mysecond'
);
echo $mypages['first'];

Multi Dimensional Array 

Although the the example below looks more confusing than the example above, the two are very similar. The main difference is that some of the items in the array called 'myarray' are arrays. Essentially, you have an array with some single items while the 'multi' item also contains an array and one of its items contains an array.

To get your desired output, you simply start with the main array and work down the ladder with the various nested keys. For example, the coding $myarray["multi"]["dimensional"]["array"] contains the multi, dimensional and array keys which represent the value called 'foo'.

$myarray = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

var_dump($myarray["foo"]);
echo "
"; var_dump($myarray[42]); echo "
"; var_dump($myarray["multi"]["dimensional"]["array"]); echo $myarray["multi"]["dimensional"]["array"];

Another Multidimensional Array 

This example is like the example above, except the 'dimensional' key contains an array with only names. The values for the array mice, cat and dog are represented with the numerical keys 0,1,2. This is the reason why printing $myarray3["multi"]["dimensional"][1] outputs cat.

$myarray3 = array(
       "multi" => array(
         "dimensional" => array("mice","cat","dog")
         )
    );

echo "Array 3: ". $myarray3["multi"]["dimensional"][1]; // cat;

Multidimensional Array With Indexed Entries 

This example is like the example above, except there are numerical keys written for the dimensional array. The values for the array mice, cat and dog are also represented with the numerical keys 0,1,2. This is the reason why printing $myarray3["multi"]["dimensional"][1] outputs cat.

$myarray4 = array(
       "multi" => array(
         "dimensional" => array(0 => "a", 1 => "b", 2 => "c", "myword" => "test")
         )
    );

echo "Array 4: ". $myarray4["multi"]["dimensional"][1]; // cat
echo "
"; echo "Array 4b: ". $myarray4["multi"]["dimensional"]["myword"]; // cat

Accessing Elements In an Array With Numerical Keys or Named Keys 

This example is like the example above, except that the numerical indexes are gone. Without a key defined with '=>', any item takes on a numerical key.

$myarray5 = array(
       "multi" => array(
         "dimensional" => array("a", "b", "c", "myword" => "test2")
         )
    );

echo "Array 5: ". $myarray5["multi"]["dimensional"][1]; // cat
echo "
"; echo "Array 5b: ". $myarray5["multi"]["dimensional"]["myword"]; // cat

At this point, you should have some idea about arrays in regards to keys, values and usage.