Introduction
A multidimensional array is an array containing one or more arrays.
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
The dimension of an array indicates the number of indices you need to select an element.
- For a two-dimensional array you need two indices to select an element
- For a three-dimensional array you need three indices to select an element
Ex:
1 2 3 4 5 6 7 8 9 10 | <?php $tasks = [ ['Learn PHP programming', 2], ['Practice PHP', 2], ['Work', 8], ['Do exercise', 1], ]; print_r($todo_list); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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 ) ) |
Adding elements
Syntax:
<?php
$array[] = [element1, element2, …];
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $tasks = [ ['Learn PHP programming', 2], ['Practice PHP', 2], ['Work', 8], ['Do exercise', 1], ]; $tasks[] = ['Build something matter in PHP', 2]; print_r($tasks ); |
Removing elements
To remove an element from a multidimensional array, you can use the unset() function.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $tasks = [ ['Learn PHP programming', 2], ['Practice PHP', 2], ['Work', 8], ['Do exercise',1], ]; unset($tasks[2]); print_r($tasks); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Array ( [0] => Array ( [0] => Learn PHP programming [1] => 2 ) [1] => Array ( [0] => Practice PHP [1] => 2 ) [3] => Array ( [0] => Do exercise [1] => 1 ) ) |
The unset() function doesn’t change the array’s keys. To reindex the key, you can use the array_splice() function.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $tasks = [ ['Learn PHP programming', 2], ['Practice PHP', 2], ['Work', 8], ['Do exercise', 1], ]; array_splice($tasks[2], 2, 1); print_r($tasks); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Array ( [0] => Array ( [0] => Learn PHP programming [1] => 2 ) [1] => Array ( [0] => Work [1] => 8 ) [2] => Array ( [0] => Do exercise [1] => 1 ) ) |
Iterating over elements
To iterate a multidimensional array, you use a nested foreach loop like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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>'; } } |
Output:
1 2 3 4 5 6 7 8 | Learn PHP programming 2 Practice PHP 2 Work 8 Do exercise 1 |
Accessing elements
To access an element in an multidimensional array, you use the square brackets ([]):
Syntax:
<?php
$array[key][key][key]…
Ex:
1 2 3 4 5 6 7 8 9 10 | <?php $tasks = [ ['Learn PHP programming', 2], ['Practice PHP', 2], ['Work', 8], ['Do excercise', 1], ]; echo $tasks[0][1]; |
Sorting a multidimensional array
To sort a multidimensional array, you use the usort() function.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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 ) ) |