Two Dimensional Array |Multidimensional-PHP Tutorials

What is two dimensional Array in PHP ?

In the previous PHP tutorial,we have discussed about array and Types of Arrays.In the array, we have discussed the array can contain only one array for list of values.If you need to use more values for more keys.

In the two dimensional, each element of the main array can be an array and the sub-element of the sub-array can be an array. It is the same process for 3,4,5, dimensional array. That is known as a multidimensional array.

Definition and Use of two dimensional Array

The two-dimensional array has rows and columns .
The Two-dimensional array is a list of one-dimensional array. To declare two-dimensional array of size [x][y]. The Two dimensional array based on each element in the main array can be an array.


Example of the Two Dimensional Array

The array() function is also used to create the two dimensional array

Example

<?php 
$x=array('Vishal'=>array('20','1997','MCA'),
         'Manish'=>array('25','1992','M-Tech'));
		 echo $x['Vishal'][2];
		 
?>
Run

In the above example, we have an array in which Vishal's age 20, the year 1997 and qualification MCA same like for Manish's age 25, the year 1992, qualification M-tech .

We declared a variable $x for the array. After that if we have to need to find out What is the qualification of Vishal then we will use the Two-dimensional array as above example .

[Vishal] [2]. We have written an instruction that fetches qualification of Vishal that is stored index number 2.
[2] is an index number of the array. .
In another example we will find the Manish Class .

Example

<?php 
$x=array('Vishal'=>array('age'=>'20','age'=>'1997','class'=>'MCA'),
         'Manish'=>array('age'=>'20','age'=>'1992','class'=>'M-Tech'));
		 echo $x['Manish']['class'];
?>
Run

Find out the class of Manish using Two dimensional Array.

What is exact meaning of Multidimensional Array ?

As before we have discussed two-dimensional array. The Multidimensional array can contain one or more arrays. A multidimensional array is an array containing one or more arrays.

In PHP, you can use two, three, four five dimensional array.After three dimensional array ,it becomes complex for normal person

Definition and Usage of Multidimensional Array

A multidimesional array stands for the store values with more than one key. The multidimensional array can contain multiple arrays inside. One of the major advantages of the multidimensional array, each element in the main array can be an array. And each element in the sub-array can be an array and also increase the same process for other elements.

Another example of two dimensional Array can be multi-dimensional.

This example is of array you can create numeric array in the same way . In the below example, we create a two-dimensional array for store three student data. Here, we store three students with three subject numbers. In this example, we use the associative array. As you know that associative array is based on the key and its value . key =>value.

Associative array helps us to remove more index complexity. That indicates direct value. try this two dimensional array .

<?php 
         $numbers = array( 
            "Ram" => array (
               "english" => 67,
               "hindi" => 90,	
               "maths" => 58
            ),
            
            "Syam" => array (
               "english" => 89,
               "hindi" => 78,
               "maths" => 80
            ),
            
            "Geeta" => array (
               "english" => 70,
               "hindi" => 68,
               "maths" => 91
            )
         );
echo "Ram numbers in Maths = ".$numbers['Ram']['maths'];
echo"
"; echo "Syam numbers in Maths = ".$numbers['Syam']['maths']; echo"
"; echo "Geeta numbers in Maths = ".$numbers['Geeta']['maths']; ?>
	
Ram numbers in Maths = 58
Syam numbers in Maths = 80
Geeta numbers in Maths = 91

In the above example, we created a two-dimensional array. You can process for further 3, 4,5(Multidimensional Array), etc. In the first array, Ram is an element but Ram can be an array.

The same thing applied to Syam and Geeta. These are also the main array element but can be an array. Increase this process for the sub-array. The element English, Hindi, maths can be an array that is known as a multidimensional array. It's very complex to understand the normal person.