PHP Multidimensional Array
An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store the data in the form of two dimensional arrays.
Emp-id | Name | Salary |
John | 20 | 30000 |
Helena | 30 | 60000 |
Rasmus | 29 | 40000 |
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php $emp = array ( array(1,"john",300000), array(2,"Helena",60000), array(3,"Rasmus",40000) ); //traversal 2D elements of an array in the form of (row*column) for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col]." "; } echo "<br/>"; } ?> </body> </html>Output 1 john 300000 2 Helena 60000 3 Rasmus 40000 Example2
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php error_reporting(1); $emp=array(array("emp_id"=>1,"name"=>"John","mob"=>9143434323), array("emp_id"=>2,"name"=>"Helena","mob"=>9134433235), array("emp_id"=>3,"name"=>"Rasmus","mob"=>9133534323) ); echo '<table border="3">'; echo '<tr>'; echo '<td align="center">Employee Id</td>'; echo '<td align="center">Employee Name</td>'; echo '<td align="center">Mobile no</td>'; foreach($emp as $k){ echo '<tr>'; foreach($k as $v){ echo '<td align="center">'.$v.'</td>'; } echo '</tr>'; } echo '</table>'; ?> </body> </html>Output Example 3
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <form method="get"> Select Country:<select name="c"> <option value="ind">india</option> <option value="pak">Pakistan</option> <option value="ch">Australia</option> </select> <input type="submit" value="submit" name="display"/> </form> <?php $country=array( "ind"=>array("Andhra Pradesh","Arunachal Pradesh","Assam","Bihar","Rajastha"), "pak"=>array("Islamabad","Lahore"), "ch"=>array("Victoria","Queensland","SouthAustralia","WesternAustralia","New South Wales") ); if(isset($_GET['display'])){ $get_country=$_GET['c']; echo "<br>"."Select City "; foreach($country as $country_key => $cname){ if($country_key==$get_country){ echo "<select>"; foreach($cname as $state){ echo "<option>".$state."</option>"; } echo "</select>"; } } } ?> </body> </html>Output