PHP - Multidimensional Arrays

Drupal loves multidimensional or nested arrays.  Drupal really loves them.  To understand multidimensional arrays, you must have a first understanding of associative arrays.

In the simplest sense, multidimensional arrays are arrays within arrays.  First, take a look at the following code.

$GaryColeman= array (
    'occupation' => 'actor',
    'decade' => '1980s',
    'show' => 'Different Strokes',
    'status' => 'deceased',
);

This is a standard associative array, but take a look at the following code.

$GaryColeman= array (
    'occupation' => 'actor',
    'decade' => '1980s',
    'show' => 'Different Strokes',
    'status' => 'deceased',
);

$DanaPlato= array (
    'occupation' => 'actor',
    'decade' => '1980s',
    'show' => 'Different Strokes',
    'status' => 'deceased',
);

$ToddBridges= array (
    'occupation' => 'actor',
    'decade' => '1980s',
    'show' => 'Different Strokes',
    'status' => 'alive',
);

 

These arrays are very very similar and would be perfectly happy nested inside an multidimensional array. To nest the arrays, take a look at the following code.

$CastOfDifferentStrokes= array ($GaryColeman, $DanaPlato, $ToddBridges);

This will take the independent arrays and incorporate them into or nest them into a multidimensional array.

To access the information in this array try this:

echo $CastOfDifferentStrokes['ToddBridges']['status'];

 This would print: alive.