PHP Tutorial (& MySQL) #8 – Multidimensional Arrays
In this tutorial, we’ll be taking a look at multidimensional arrays. We’ll start by creating a multidimensional array, and then we’ll explore some of the ways that we can access and use the data in the array.
Creating a Multidimensional Array
To create a multidimensional array, we simply need to use multiple arrays within a single array. Here’s an example:
$multi = array(
array(
‘name’ => ‘John’,
‘age’ => 30,
),
array(
‘name’ => ‘Mary’,
‘age’ => 25,
),
array(
‘name’ => ‘Joe’,
‘age’ => 45,
),
);
In this example, we have an array called $multi. This array contains three arrays, each of which contains information about a different person.
Accessing Data in a Multidimensional Array
We can access the data in a multidimensional array in a number of different ways. Here are a few examples:
$multi[‘name’]
This will return the name of the first person in the array.
$multi[1][‘name’]
This will return the name of the second person in the array.
$multi[2][‘age’]
This will return the age of the third person in the array.
As you can see, we can access the data in a multidimensional array in a number of different ways. This makes it easy to access the information that we need.