PHP Multidimensional Arrays

Multidimensional Arrays in PHP are a type of arrays which store another array in each index rather than a single element. In other words, it is an array of arrays. Therefore, every element of the array may contain an element or another sub-array and so on. Also, we require more than one dimensions to access the elements. In this article, we will discuss the PHP Multidimensional Arrays. Also, we will discuss examples of creating, traversing and accessing them.

Multiple Dimensions in Arrays

The number of dimensions indicates the number of indices required to access an element. You can create an array of any number of dimensions. However, generally, developers find it complicated to work with more than 3 dimensions in a single array.

  • Two Dimensional Array: You need two indices to access an element.
  • Three Dimensional Array: You need three indices to access an element.

Two Dimensional Array in PHP

To access an element in a Two Dimensional Array, you require 2 indices. This is the simplest form of Multidimensional Arrays in PHP. Also, as usual, the indexes start from 0.

Syntax

array (
	array(element1, element2, ... elementN),
	array(element1, element2, ... elementN),
	...
	array(element1, element2, ... elementN)
);

Each element in a Two Dimensional Array contains another sub-array. This can be represented in the following diagram:

PHP Multidimensional Arrays with 2 Dimensions

Initialising a Two Dimensional Array

<?php
	
	//Creating a Two Dimensional Array in PHP
	$twoDimensionalArray = array(
		array('Jack', 10, 20),
		array('Rose', 30, 5),
		array('Emily', 40, 10)
	);
	print_r($twoDimensionalArray);

?>

In the above example, the array contains another sub-array in each of its indices. This is an example of creating a Two Dimensional array in PHP. The above example will print the following:

Array
(
    [0] => Array
        (
            [0] => Jack
            [1] => 10
            [2] => 20
        )
    [1] => Array
        (
            [0] => Rose
            [1] => 30
            [2] => 5
        )
    [2] => Array
        (
            [0] => Emily
            [1] => 40
            [2] => 10
        )
)

Accessing Elements in Two Dimensional Array

You can use two indices to access the elements in a two dimensional array in PHP. For instance, consider the example below:

<?php
	
	//Creating a Two Dimensional Array in PHP
	$twoDimensionalArray = array(
		array('Jack', 10, 20),
		array('Rose', 30, 5),
		array('Emily', 40, 10)
	);

	echo 'The Element at Position 0,0 is: '.$twoDimensionalArray[0][0];
	echo 'The Element at Position 1,2 is: '.$twoDimensionalArray[1][2];
	echo 'The Element at Position 2,1 is: '.$twoDimensionalArray[2][1];
OUTPUT:
The Element at Position 0,0 is: Jack
The Element at Position 1,2 is: 5
The Element at Position 2,1 is: 40

Remember: The indices always start from 0 and not 1.

Two Dimensional Associative Array

Similarly, you can create a Two Dimensional Associative Array using string keys.

<?php
	
	//Creating a Two Dimensional Array in PHP
	$twoDimensionalArray = array(
		'person1' => array('name' => 'Jack', 'age' => 10, 'score' => 20),
		'person2' => array('name' => 'Rose', 'age' => 30, 'score' => 5),
		'person3' => array('name' => 'Emily','age' => 40, 'score' => 10)
	);
	print_r($twoDimensionalArray);
OUTPUT:
Array
(
    [person1] => Array
        (
            [name] => Jack
            [age] => 10
            [score] => 20
        )
    [person2] => Array
        (
            [name] => Rose
            [age] => 30
            [score] => 5
        )
    [person3] => Array
        (
            [name] => Emily
            [age] => 40
            [score] => 10
        )
)

Likewise, you can access the elements of the above array using the a pair of keys.

echo 'Person 2 Name: '.$twoDimensionalArray['person2']['name'];
echo 'Person 2 Age: '.$twoDimensionalArray['person2']['age'];
echo 'Person 2 Score: '.$twoDimensionalArray['person2']['score'];
Person 2 Name: Rose
Person 2 Age: 30
Person 2 Score: 5

Three Dimensional Arrays

You can also create PHP Multidimensional Arrays with three dimensions. Therefore, you need three indices to access elements of a three-dimensional array.

PHP Multidimensional Arrays with 3 Dimensions

Initialising a Three Dimensional Array

<?php
	
	//Creating a Three Dimensional Array in PHP
	$threeDimensionalArray = array(
		array(
			array('Jaguar', 'BMW', 'Audi'),
			array('Honda', 'Hyundai', 'Skoda')
		), 
		array(
			array('City', 'Jazz'),
			array('i20', 'i10', 'Verna', 'Creta')
		)
	);
	print_r($threeDimensionalArray);
?>

Similarly, we can create a three-dimensional array in PHP using three indices. In the above example, each element of an array contains an array, containing another sub-array. In other words, each element of a three-dimensional array contains a two dimension array.

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Jaguar
                    [1] => BMW
                    [2] => Audi
                )
            [1] => Array
                (
                    [0] => Honda
                    [1] => Hyundai
                    [2] => Skoda
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => City
                    [1] => Jazz
                )
            [1] => Array
                (
                    [0] => i20
                    [1] => i10
                    [2] => Verna
                    [3] => Creta
                )
        )
)

Accessing Elements in Three Dimensional Array

Similarly, you can access array elements using three indices.

echo 'Element 0,0,0: '.$threeDimensionalArray[0][0][0];
echo 'Element 0,2,1: '.$threeDimensionalArray[0][1][1];
echo 'Element 2,1,2: '.$threeDimensionalArray[1][1][1];
OUTPUT:
Element 0,0,0: Jaguar
Element 0,2,1: Hyundai
Element 1,1,1: i10

Accessing Elements in Multidimensional Arrays in PHP

There are two ways of accessing elements in PHP Multidimensional Arrays.

  1. You can access each element of the array using indices. The number of indices depends on the number of dimensions in the array.
  2. Also, you can access the elements using loops.

Traversing Multidimensional Arrays in PHP Using Loop

For instance, consider the following example.

<?php
	
	//Creating a Two Dimensional Array in PHP
	$twoDimensionalArray = array(
		array('Jack', 10, 20),
		array('Rose', 30, 5),
		array('Emily', 40, 10)
	);

	for ($i = 0; $i < 3; $i++) {
		for ($j = 0; $j < 3; $j++) {
			echo 'Element at '.$i.','.$j.' is: '.$twoDimensionalArray[$i][$j];
		}
	}
?>

In the above example, we traverse the two dimensional array using 2 for loops.

Element at 0,0 is: Jack
Element at 0,1 is: 10
Element at 0,2 is: 20
Element at 1,0 is: Rose
Element at 1,1 is: 30
Element at 1,2 is: 5
Element at 2,0 is: Emil
yElement at 2,1 is: 40
Element at 2,2 is: 10

Conclusion

In conclusion, we discussed PHP Multidimensional Arrays. Also, you can read more about them on the Official PHP Documentation. Additionally, you can also read PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *