Zend PHP 7 Certification – Arrays – Multidimensional Arrays

This post covers the Multidimensional Arrays section of the Arrays chapter when studying for the Zend PHP 7 Certification.

A multidimensional array is an array containing two or more arrays. The array could look like the below example.

<?php
$array = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

We can then get the value foo by writing the below code.

<?php echo $array['multi']['dimensional']['array']; // Outputs: foo

This is useful when you want to store data that has a more complicated structure than just a key and a single value.

Each level of an array is called a dimension. So an array with two levels is called a two-dimensional array, three levels is a three-dimensional array and so on.

Usually arrays won’t go beyond three levels as they can become complicated to manage.

// Two-dimensional array
$array = array(
    array(0, 1, 2),
    array(3, 4, 5),
);

echo $array[0][1]; // Outputs: 1
echo $array[1][2]; // Outputs: 5

// Three-dimensional array
$cars = array(
    "manufacturers" => array(
        "ford" => array(
            "fiesta" => 6000,
            "mustang" => 15000
        ),
        "honda" => array(
            "civic" => 8000,
            "s2000" => 12000
        ),
        "toyota" => array(
            "supra" => 11000,
            "celica" => 9000
        )
    )
);

echo $cars['manufacturers']['ford']['fiesta']; // Outputs: 6000

As you can see you need two indexes to select a value from the second level of a two-dimensional array, and three indexes to select a value from the third level of a three-dimensional array. The dimension of an array indicates the number of indexes you need to select an element.

Note that you can also use for and foreach loops to print out the values from multi-dimensional arrays.

Here’s an example of a for loop.

$fruit = array(
    array(
        'Apples', 
        'Bananas',
        'Oranges'
    ),
    array(
        'Grapes',
        'Strawberries',
        'Melons'
    )
);

// $num_fruit is 2: the number of elements in the first dimension of $fruit
for ($i = 0, $num_fruit = count($fruit); $i < $ ; $i++) {
    // $num_sub is 3: the number of elements in each sub-array
    for ($m = 0, $num_sub = count($fruit[$i]); $m < $num_sub; $m++) {
        print "Element [$i][$m] is " . $fruit[$i][$m] . "\n";
    }
}

This outputs the following.

Element [0][0] is Apples
Element [0][1] is Bananas
Element [0][2] is Oranges
Element [1][0] is Grapes
Element [1][1] is Strawberries
Element [1][2] is Melons

Here is what a foreach loop example might look like.

$flavours = array(
    'Japanese' => array(
        'hot' => 'wasabi',
        'salty' => 'soy sauce'
     ),
     'Chinese'  => array(
         'hot' => 'mustard',
         'pepper-salty' => 'prickly ash'
     )
);

// $culture is the key and $culture_flavours is the value (an array)
foreach ($flavours as $culture => $culture_flavours) {

    // $flavour is the key and $example is the value
    foreach ($culture_flavours as $flavour => $example) {
        print "A $culture $flavour flavour is $example.\n";
    }
}

This outputs the following.

A Japanese hot flavour is wasabi.
A Japanese salty flavour is soy sauce.
A Chinese hot flavour is mustard.
A Chinese pepper-salty flavour is prickly ash.

View the other sections:

Note: This article is based on PHP version 7.0.