What is multidimensional arrays in php ?

Multi Dimensional arrays and its manipulation : –

  • In general the arrays can be defined as the  collection of similar type of values in a single variable .
  • The arrays can be the collection of anything such as string , integers , objects as well as arrays itself .
  • Thus , holding the arrays with in the arrays deals with multidimensional or nested arrays , we can have any number of the multidimensional arrays .
  • But generally the two-dimensional and three-dimensional arrays are used as multidimensional array .
  • The multidimensional array can be either indexed of associative arrays .

So , Let’s with the example of multidimensional arrays .

Syntax :

$multiArray = array(
         array( value1, value2, value3 ),
         array( value4, value5, value6 ),
         array( value7, value8, value9 )
   );


Let’s example for second level of  multidimensional array :

<?php 

$books  = array(
  array(
    “title” => “PHP Basic”,
    “author” => “Samon Pal”,
    “published” => 2006
  ),
  array(
    “title” => “PHP Advance”,
    “author” => “Peter Marteen”,
    “published” => 2007
  ),
  array(
    “title” => “PHP Expert”,
    “author” => “Joe Kubrick”,
    “published” => 2007
  )
);


//displaying the array elements manually

echo $books[0][‘title’].” is written by “.$books[0][‘author’].” in “.$books[0][‘published’].”<br>”;
echo $books[1][‘title’].” is written by  “.$books[1][‘author’].” in  “.$books[1][‘published’].”<br>”;
echo $books[2][‘title’].” is written by  “.$books[2][‘author’].” in  “.$books[2][‘published’].”<br><br>”;


//displaying the array elements vis loop

foreach ($books as $key => $value) {
  
   echo $value[‘title’].” is written by “.$value[‘author’].” in “.$value[‘published’].”<br>”;
}

?>

Here in above example the keys ( 0,1, 2  in 1st ) and  $key in second are the root key element of each array and (title , author , published ) are the associative keys for nested array of  multidimensional array  $books
 example and will produce asme example as …

PHP Basic is written by Samon Pal in 2006
PHP Advance is written by Peter Marteen in 2007
PHP Expert is written by Joe Kubrick in 2007


Similarly ,

In Same way the 3, 4 , 5 .. nth dimensional (multidimensional ) works .
For any query , Please leave a comments ………….

Enjoy codding ……………

Leave a Reply

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