Thursday, September 14, 2017

PHP Multidimensional Array


PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.

Code Snippet to write multidimensional array
$student = array  
  (  
  array(1,"Sumit",25),  
  array(2,"Amit",45),  
  array(3,"Neeraj",32)  
  );

Multidimensional Array Example :


Let's see a simple example of PHP multidimensional array to display following tabular data. In this example, we are displaying 3 rows and 3 columns including the header of the table.

 PHP Multidimensional Array
<?php    
$student = array  
  (
  array("ID","NAME","AGE"),      
  array(1,"Sumit",25),  
  array(2,"Amit",45),  
  array(3,"Neeraj",32)  
  );    
  
for ($row = 0; $row < 4; $row++) {  
  for ($col = 0; $col < 3; $col++) {  
    echo $student[$row][$col]." | ";  
  }  
  echo "<br/>";  
}  
?>

Output:
------------------------
ID | NAME | AGE |
1 | Sumit | 25 |
2 | Amit | 45 |
3 | Neeraj | 32 |




No comments:

Post a Comment