How to Access the Elements of a Multidimensional Array in PHP

In this article, we show how to access any individual element or any elements of a multidimensional array in PHP.
A multidimensional array is an array that contains arrays.
So let's say we have created the following multidimensional array below composed of people: their name, email, city,
and state. The second block of PHP shows how to access all of the data in the array.
So we've created a multidimensional array composed of individual people, holding their name, email, city and state.
We then access every individual element in the array. We do this using the array name, $people followed by the row number, followed by the name of the column we want to access (name, email, city, or state).
This is how the individual element of a multidimensional array are accessed.
Running the above PHP code, we get the following output below.
Actual PHP Output
Jennifer Kimbers
abc@gmail.com
Seattle
Washington
Rodney Hutchers
def@gmail.com
Los Angeles
California
Robert Smith
ghi@gmail.com
Michigan
Missouri
So this is how the elements of a multidimensional array can be accessed that has key-value pairs.
We will show now below to access the elements of multidimensional array that does not have key-value pairs.
Accessing the Elements of a Multidimensional Array Without Key-Value Pairs
So now we show how to access the elements of a multidimensinal array that does not have key-value pairs.
The PHP code below creates the array and outputs the elements of the array.
So this is how a multidimensional can be accessed that does not have key-value pairs.
We use the name of the array $people, followed by the row number, followed by the column number.
So $people[0][0] corresponds to the first row of the first column. Since arrays start at 0, 0 begins the array. This corresponds to the name, Jennifer Kimbers.
You can see the difference of accessing multidimensional arrays with key-value pairs and those without. Usually those with key-value pairs are preferred because the column can be referenced by name instead of by number. But a programmer may have his or her preference.
Running the PHP code above, we get the following output shown below.
Actual PHP Output
Jennifer Kimbers
abc@gmail.com
Seattle
Washington
Rodney Hutchers
def@gmail.com
Los Angeles
California
Robert Smith
ghi@gmail.com
Michigan
Missouri
And this is all that is required to access elements of a multidimensional array.
Related Resources
How to Loop
Through a Multidimensional Array in PHP