This article will show you how to use multidimensional arrays in the PHP programming language, explaining what they are and how to use them.
What is an Array?
An array is a type of PHP variable that can hold multiple values. It’s a list of values you can loop through and perform operations on each individual value.
For example, you might want to perform an action on a list of files. By storing that list as an array, you can loop through the file names in it and perform the action on each one.
Arrays are indexed, with the position of each item in the array being represented by a number starting at 0.
What is a Multidimensional Array?
An array is multidimensional if it has more than one dimension or aspect – That is to say, it’s not just a simple list (1 dimensional), but a list of lists (two dimensional).
An item in a multidimensional array has two coordinates – the position in the first list and the position in the second list.
A two-dimensional array is analogous to a table with rows and columns – and thus X and Y coordinates. The row is the item’s index in the first array, which contains the columns that define the items Y coordinate.
Defining Arrays
Arrays can be constructed in two different ways in PHP – in long-form using the array function:
$coloursArray = array("blue", "green", "purple", "orange");
Or in short-form, using square brackets:
$coloursArray = ["blue", "green", "purple", "orange"];
The following examples will use the latter, as it’s simpler and more readable.
Defining Multidimensional Arrays in PHP
Defining a multidimensional array is easy. Define an array, and fill it with other arrays.
The following defines a multidimensional array of fruit, with some information about each (name, color, weight in kilograms):
$fruit = [ ["Apple", "Green", 0.3], ["Banana", "Yellow", 0.3], ["Grape", "Purple", 0.01], ["Tomato", "Red", 0.2] ];
The outer set of square brackets defines the first dimension of the multidimensional array – the rows.
Each row is its own array, containing the information for each column – the second dimension of the multidimensional array.
By using the square brackets instead of the longer array() function to construct the arrays, it’s easy to read the array and see that it is, in effect, a table of data with discernable rows and columns.
Arrays can be as long or as short as you require.
You can nest arrays as deeply as you need to as well; you could have an array value inside each individual fruit array containing more data, adding further dimensions.
Accessing and Using Multidimensional Arrays in PHP
To access a value in an array, simply specify the position of the value at each coordinate/dimension. This will be the index of the value in the array, so counting starts at 0.
For example:
$fruit = [ ["Apple", "Green", 0.3], ["Banana", "Yellow", 0.3], ["Grape", "Purple", 0.01], ["Tomato", "Red", 0.2] ]; echo $fruit[0][1]; // Colour of apple echo $fruit[2][2] // Weight of grape echo $fruit[1][0] // Name of banana
Printing Multidimensional Arrays
The print_r function will recursively print out a PHP array:
print_r($fruit[3]); // Print all details regarding tomato print_r($fruit); // Print details on all fruit
Find out more about print_r and var_dump for printing arrays here.