PHP Multidimensional Array

PHP Multidimensional Array means if an array presents inside another array then its called two dimensional array. Three-dimensional array is an array containing array of arrays. If you want to learn about basic of array and different types of array, then read this article on PHP Array. Here I’ll create upto three dimensional array, I’ll take a simple data of a person name and his age and date of birth in an array.

Name Age Date of Birth
Raju 20 1994

This is the code for this array

<?php
array("Raju",20,1994);
?>

Two dimensional Array

If an array contains in an array, then that is called as two dimensional array. Here is the example data and code for an example array.

Name Age Date of Birth
Raju 20 1994
Ramu 24 1990
Rani 30 1984
$persons = array
  (
  array("Raju",20,1994),
  array("Ramu",24,1990),
  array("Rani",30,1984)
  );

Now the two-dimensional $persons array contains three arrays, and it has two indices: row and column. To get access to the elements of the $persons array we must point to the two indices (row and column):

<?php
echo $persons[0][0]."'s age is ".$persons[0][1].", and date of birth is ".$persons[0][2].".<br>";
echo $persons[1][0]."'s age is ".$persons[1][1].", and date of birth is ".$persons[1][2].".<br>";
echo $persons[2][0]."'s age is ".$persons[2][1].", and date of birth is ".$persons[2][2].".<br>";
?>

Three Dimensional Array & PHP Multidimensional Array

For making three dimensional array, I’ll make change for above array. This is an example of three dimensional array.

$persons = array(array(array("Raju", 20, 1994),
                    array("Ramu", 24, 1990),
                    array("Rani", 30, 1986) 
                   ),
              array(array("Raju", 20, 1994),
                    array("Ramu", 24, 1990),
                    array("Rani", 30, 1986) 
                   ),
              array(array("Raju", 20, 1994),
                    array("Ramu", 24, 1990),
                    array("Rani", 30, 1986) 
                   )
             );

PHP Multidimensional Array means it can have any number of array inside an array.

Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe