PHP Multidimensional Array in Numeric Indexed Value Example

PHP Multidimensional Array

PHP Multidimensional Array means array inside an array. Or An array containing more than one array. Its structure behaves like metrics.

In Multidimensional array, the data are stored in a tabular form (row-column formate).

Syntax of Multidimensional Array in PHP

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$arrayName = array ( array(value1,value2,value3));	
	?>
  </body>
</html>

Note:The var_dump() and print_r() are the two functions which are used to debug and analyze the content and the structure of the array.

Example 1 – Multidimensional Array Example to store name, subject name and marks.

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array (
					array("Peter","Maths",89),	
					array("Peter","Physics",66),
					array("Juliet","Maths",99),
					array("Juliet","Physics",74)
				);
				echo $marks[0][0] . " " . $marks[0][1] . " " . $marks[0][2] . "<br />";
				echo $marks[1][0] . " " . $marks[1][1] . " " . $marks[1][2] . "<br />";
				echo $marks[2][0] . " " . $marks[2][1] . " " . $marks[2][2] . "<br />";
				echo $marks[3][0] . " " . $marks[3][1] . " " . $marks[3][2];
	?>
  </body>
</html>

The above example we used echo function to show the content of the array to the output screen. The below screenshot define how the output is shown on the output screen.

Multidimensional Array in PHP with echo example code syntax
Fig.1 – PHP Multidimensional Array with echo Example.

Example 2 – Multidimensional Array with var_dump() function Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$marks = array (
					array("Peter","Maths",89),	
					array("Peter","Physics",66),
					array("Peter","Chemistry",73),
					array("Juliet","Maths",99),
					array("Juliet","Physics",74),
					array("Juliet","Chemistry",72),
					array("John","Maths",75),
					array("John","Physics",50),
					array("John","Chemistry",45)
				);
				var_dump($marks);
	?>
  </body>
</html>

The above example we used var_dump() to show the content of the array to the output screen. The below screenshot define how the output is shown on the output screen.

PHP Multidimensional Array
Fig.2 – PHP Multidimensional Array.

Caution: With the use of pre tag with the var_dump (echo “<pre>”; var_dump(); echo “</pre>”;) output will show in a well-structured form.

You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape