Last Update - 09 Jun 2020

How to create multidimensional Array in php

How to create multidimensional Array in php

We know an array is a special variable, which can hold more than one value in one single variable. In php array can be used multiple types which can be single or simple array and multidimensional array.

 

Single or simple array :

<?php

$array = array("Jhon","Tony","Hulk","Ram");
echo  '<pre>';
print_r($array);

?> 

 

Result of this array is:

Array
(
    [0] => Jhon
    [1] => Tony
    [2] => Hulk
    [3] => Ram
)
 

 

And multidimensinal array have array of array:

<?php

$array = array("Jhon" => array("age" => 32,
							   "department" => "IT"
							  ),
			   "Tony" => array("age" => 42,
							   "department" => "science"
							  ),
			   "Hulk" => array("age" => 46,
							   "department" => "security"
							  ),
			   "Mohan" => array("age" => 22,
							   "department" => "sales"
							  )
			  );
echo  '<pre>';
print_r($array);

?> 

 

Result of this array:

Array
(
    [Jhon] => Array
        (
            [age] => 32
            [department] => IT
        )

    [Tony] => Array
        (
            [age] => 42
            [department] => science
        )

    [Hulk] => Array
        (
            [age] => 46
            [department] => security
        )

    [Mohan] => Array
        (
            [age] => 22
            [department] => sales
        )

)
 

Did you find this page helpful? X