Summary: in this tutorial, you will learn how to define a PHP multidimensional array and manipulate its elements effectively.
Introduction to PHP multidimensional array
Typically, you have an array with one dimension. For example:
<?php
$scores = [1, 2, 3, 4, 5];
Code language: HTML, XML (xml)
Or
<?php
$rates = [
'Excellent' => 5,
'Good' => 4,
'OK' => 3,
'Bad' => 2,
'Very Bad' => 1
];
Code language: HTML, XML (xml)
Both $scores
and $rates
are one-dimensional arrays.
A multidimensional array is an array that has more than one dimension. For example, a two-dimensional array is an array of arrays. It is like a table of rows and columns.
In PHP, an element in an array can be another array. Therefore, to define a multidimensional array, you define an array of arrays.
The following example uses an array of arrays to define a two-dimensional array:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do exercise' 1],
];
Code language: HTML, XML (xml)
In the $tasks
array, the first dimension represents the tasks and the second dimension specifies hour spent for each.
To dispaly all the elements in a multidimensional array, you use the print_r()
function like this:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do exercise', 1],
];
print_r($todo_list);
Code language: HTML, XML (xml)
Output:
Array
(
[0] => Array
(
[0] => Learn PHP programming
[1] => 2
)
[1] => Array
(
[0] => Practice PHP
[1] => 2
)
[2] => Array
(
[0] => Work
[1] => 8
)
[3] => Array
(
[0] => Do exercise
[1] => 1
)
)
Code language: PHP (php)
Adding elements to a PHP multidimensional array
To add an element to a multidimensional array, you use the the following syntax:
<?php
$array[] = [element1, element2, ...];
Code language: HTML, XML (xml)
For example, to add an element at the end of the $tasks
array, you use the following:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do exercise', 1],
];
$tasks[] = ['Build something matter in PHP', 2];
print_r($tasks );
Code language: HTML, XML (xml)
Removing elements from a PHP multidimensional array
To remove an element from a multidimensional array, you can use the unset()
function.
The following example uses the unset()
function to remove the third element of the $tasks array:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do exercise',1],
];
unset($tasks[2]);
print_r($tasks);
Code language: HTML, XML (xml)
Output:
Array
(
[0] => Array
(
[0] => Learn PHP programming
[1] => 2
)
[1] => Array
(
[0] => Practice PHP
[1] => 2
)
[3] => Array
(
[0] => Do exercise
[1] => 1
)
)
Code language: PHP (php)
Note that the unset()
function doesn’t change the array’s keys. To reindex the key, you can use the array_splice()
function. For example:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do exercise', 1],
];
array_splice($tasks[2], 2, 1);
print_r($tasks);
Code language: HTML, XML (xml)
Output:
Array
(
[0] => Array
(
[0] => Learn PHP programming
[1] => 2
)
[1] => Array
(
[0] => Work
[1] => 8
)
[2] => Array
(
[0] => Do exercise
[1] => 1
)
)
Code language: PHP (php)
Iterating over elements of a multidimensional array using foreach
To iterate a multidimensional array, you use a nested foreach
loop like this:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do exercise', 1],
];
foreach ($tasks as $task) {
foreach ($task as $task_detail) {
echo $task_detail . '<br>';
}
}
Code language: HTML, XML (xml)
Output:
Learn PHP programming
2
Practice PHP
2
Work
8
Do exercise
1
Code language: PHP (php)
Accessing elements of a multidimensional array
To access an element in an multidimensional array, you use the square brackets ([]
):
<?php
$array[key][key][key]...
Code language: HTML, XML (xml)
For example, to access the number of hour spent for the "Learn PHP programming"
task, you use the following code:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do excercise', 1],
];
echo $tasks[0][1];
Code language: HTML, XML (xml)
Sorting a multidimensional array
To sort a multidimensional array, you use the usort() function. For example:
<?php
$tasks = [
['Learn PHP programming', 2],
['Practice PHP', 2],
['Work', 8],
['Do excercise', 1],
];
usort($tasks, function ($a, $b) {
return $a[1] <=> $b[1];
});
print_r($tasks);
Code language: HTML, XML (xml)
In this example, we use the spaceship operator (<=>
), which has been available since PHP 7, to compare the time spent for each task and sort the tasks by hours.
Output:
Array
(
[0] => Array
(
[0] => Do excercise
[1] => 1
)
[1] => Array
(
[0] => Learn PHP programming
[1] => 2
)
[2] => Array
(
[0] => Practice PHP
[1] => 2
)
[3] => Array
(
[0] => Work
[1] => 8
)
)
Code language: PHP (php)
Summary
- Use an array of arrays to create two or more multidimensional arrays in PHP.