How to Create a Multidimensional Array in PHP



PHP







In this article, we show how to create a multidimensional array in PHP.

A multidimensional array is an array that contains arrays.

So what we're really doing is we're creating an array and putting arrays in that array.

A multidimensional array is really just an array composed of arrays.

To create a multidimensional array in PHP, we following the general format below.



So let's actually create an array now composed up people, their name, phone number and email address.

We create a multidimensional array of 3 people with their name, email address, city, and state.



So the above code creates a multidimensional array in PHP.

There are 3 people in this array. We have the name, email, city, and state of each person.

So to create this multidimensional array, we created key-value pairs. The keys are name, email, city, and state. The values are whatever the name, email, city and state is of each person.

So if we use the print_r function along with the pre tags to make this more readable, we get the following output below.

Actual PHP Output


Array
(
    [0] => Array
        (
            [name] => Jennifer Kimbers
            [email] => abc@gmail.com
            [city] => Seattle
            [state] => Washington
        )

    [1] => Array
        (
            [name] => Rodney Hutchers
            [email] => def@gmail.com
            [city] => Los Angeles
            [state] => California
        )

    [2] => Array
        (
            [name] => Robert Smith
            [email] => ghi@gmail.com
            [city] => Michigan
            [state] => Missouri
        )

)
1



So you can use our array. This is creating a multidimensional array that has key-value pairs.

You can also create the same array above without key-value pairs.


Creating a Multidimensional Array Without Key-Value Pairs

To create the same array without key-value pairs, we use the following PHP code below.



If we the print_r function along with the pre tags again, we get the following output shown below.

Actual PHP Output


Array
(
    [0] => Array
        (
            [0] => Jennifer Kimbers
            [1] => abc@gmail.com
            [2] => Seattle
            [3] => Washington
        )

    [1] => Array
        (
            [0] => Rodney Hutchers
            [1] => def@gmail.com
            [2] => Los Angeles
            [3] => California
        )

    [2] => Array
        (
            [0] => Robert Smith
            [1] => ghi@gmail.com
            [2] => Michigan
            [3] => Missouri
        )

)
1



The problem with an array without key-value pairs is that it's much harder to have good organization. Since now you have represent columns with a numerical representation rather than a text representation like name, email, city, state, it's a bit harder. This is why it's better to create key-value pairs. But a programmer may prefer one way over the other. And both types of multidimensional achieve the same thing. It's just that with key-value pairs, you can call a column by name. Without key-value pairs, you have to refer to columns by numbers.

And this is all that is necessary to create multidimensional arrays in PHP.

So, for example, if we have a list of people objects that contains the city and state of the person, we can sort through this JSON data and only return people who live in the state of Florida, for instance.


Related Resources

How to Access the Elements of a Multidimensional Array in PHP

How to Loop Through a Multidimensional Array in PHP

HTML Comment Box is loading comments...