Working With Multidimensional Array in PHP

The main difference between the multidimensional array and indexed/associative array is that the indexed and associative arrays hold single element for each index or key. While, the multidimensional array holds an array for each index.

img

This makes it able to store more data, which is organized, and is easy to loop through.

Creating Multidimensional Array

<?php

$customers = array(
    array("Ali", "London", "A good customer"),
    array("John", "Miami", "An old customer"),
    array("Ana", "Syndey", "Potential customer")
    );

?>

In the example above, an array was created for customers data. As you can see, for each index, it contains another array, consisting of customer name, location, and a note.

Outputting Multidimensional Array

echo $customers[1][2];
//An old customer

print_r($customers);
//prints the whole array

The simple way to print an individual value from a multidimensional array is to print it individually using echo() as in the above mentioned example.

See the example below for the multidimensional array output into html table.

To see how array is actually created and stored, you can use print_r() functions. The output of the example above would look like this:

Array
(
    [0] => Array
        (
            [0] => Ali
            [1] => London
            [2] => A good customer
        )

    [1] => Array
        (
            [0] => John
            [1] => Miami
            [2] => An old customer
        )

    [2] => Array
        (
            [0] => Ana
            [1] => Syndey
            [2] => Potential customer
        )

)

Let's move over to using keys and values in multidimensional array.

Creating Multidimensional Array With Keys and Values

<?php

$cars = array(
    array("Make"=>"Toyota", "Model"=>"Corolla", "Color"=>"White", "Mileage"=>24000, "Status"=>"Sold"),
    array("Make"=>"Toyota", "Model"=>"Camry", "Color"=>"Black", "Mileage"=>56000, "Status"=>"Available"),
    array("Make"=>"Honda", "Model"=>"Accord", "Color"=>"White", "Mileage"=>15000, "Status"=>"Sold"),
    array("Make"=>"Nissan", "Model"=>"Juke", "Color"=>"Red", "Mileage"=>4500, "Status"=>"Sold")
    );

?>

In the example above, we created an array with the list of cars for a car dealer. Each index contains a record of 1 car, with details of manufacturer, model name, color of the car, mileage, and availability status.

To print out the single element from the array, use the echo() function like this:

echo $cars[1]["Mileage"];
//Output: 56000

And ofcourse, you can output the whole array as well using the print_r() function like this:

print_r($cars);

Array Output In HTML Table

echo "<table cellpadding='10' cellspacing'10' border='1'>";
echo "<tr> <th>Make</th> <th>Model</th> <th>Color</th> <th>Mileage</th> <th>Status</th> </tr>";
foreach($cars as $car) {
    echo "<tr> <td>$car[Make]</td> <td>$car[Model]</td> <td>$car[Color]</td> <td>$car[Mileage]</td> <td>$car[Status]</td> </tr>";
}
echo "</table>";

To output it into html table, we will actually need to print the html tags. For this, echo() is used, which prints out the table tag along with the headings.

To output each array, we are using foreach() loop here. It runs through each index, and assigns the value of the index (an array in our case) to $car. This $car is also considered an array, with keys holding each element inside it.

img

Here is the full code for you to copy/paste if you would like to try it for yourself:

<?php

$cars = array(
    array("Make"=>"Toyota", "Model"=>"Corolla", "Color"=>"White", "Mileage"=>24000, "Status"=>"Sold"),
    array("Make"=>"Toyota", "Model"=>"Camry", "Color"=>"Black", "Mileage"=>56000, "Status"=>"Available"),
    array("Make"=>"Honda", "Model"=>"Accord", "Color"=>"White", "Mileage"=>15000, "Status"=>"Sold"),
    array("Make"=>"Nissan", "Model"=>"Juke", "Color"=>"Red", "Mileage"=>4500, "Status"=>"Sold")
    );

echo "<table cellpadding='10' cellspacing'10' border='1'>";
echo "<tr> <th>Make</th> <th>Model</th> <th>Color</th> <th>Mileage</th> <th>Status</th> </tr>";
foreach($cars as $car) {
    echo "<tr> <td>$car[Make]</td> <td>$car[Model]</td> <td>$car[Color]</td> <td>$car[Mileage]</td> <td>$car[Status]</td> </tr>";
}
echo "</table>";

?>

The multidimensional array is really helpful if you are working without database to store the variety of data. Common examples are user accounts, customer details, your website's static content list (multiple elements for header, multiple elements for footer, navigation links), etc.

Multidimensional array is also helpful in fetching data from database and storing it in an organized way to use it later where needed.

For example, your database may contain the user's profile, billing details, and shipping details. By using multidimensional array, you can store each into a separate array, with a large single array.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?