Multidimensional Arrays

3. Multidimensional Arrays

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index. 

Arrays containing one or more arrays. An array can also contain another array as a value, which in turn can hold other arrays as well. In such a way we can create two- or three-dimensional arrays

Example

Array (
[Griffin] => Array  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
)
[Quagmire] => Array  (
  [0] => Glenn
 ) )

 

n this example we create a two dimensional array to store marks of three students in three subjects:

This example is an associative array, you can create numeric array in the same fashion.

<html>
<body>
<?php
   $marks = array( 
		"mohammad" => array
		(
		"physics" => 35,	    
		"maths" => 30,	    
		"chemistry" => 39	    
		),
		"qadir" => array
                (
                "physics" => 30,
                "maths" => 32,
                "chemistry" => 29
                ),
                "zara" => array
                (
                "physics" => 31,
                "maths" => 22,
                "chemistry" => 39
                )
	     );
   /* Accessing multi-dimensional array values */
   echo "Marks for mohammad in physics : " ;
   echo $marks['mohammad']['physics'] . "<br />"; 
   echo "Marks for qadir in maths : ";
   echo $marks['qadir']['maths'] . "<br />"; 
   echo "Marks for zara in chemistry : " ;
   echo $marks['zara']['chemistry'] . "<br />"; 
?>
</body>
</html>

This will produce following result:

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>