Multidimensional Arrays
This is where arrays are embedded inside the manin array. We are going to do some examples here.
<?php
$college = array (
"Conqueror" => array("department" => "Computer Sciences",
"Course" => "Software Engineering",
"grade" => "First Class"),
"Favour" => array("department" => "Medical Sciences",
"Course" => "Medicine",
"grade" => "Final year"),
"Divine" => array("department" => "Medical Sciences",
"Course" => "Medicine",
"grade" => "Final year"),
);
/* Accessing multi-dimensional array values */
echo "<h3>Record of Conqueror</h3>";
echo "Department : " ;
echo $college['Conqueror']['department'] . "<br />";
echo "Course : " ;
echo $college['Conqueror']['Course'] . "<br />";
echo "Grade : " ;
echo $college['Conqueror']['grade'] . "<br />";
echo "<p><h3>Record of Performance</h3></p>";
$college = array (
"Conqueror" => array("department" => "Computer Sciences",
"Course" => "Software Engineering",
"grade" => "First Class"),
"Favour" => array("department" => "Medical Sciences",
"Course" => "Medicine",
"grade" => "Final year"),
"Divine" => array("department" => "Medical Sciences",
"Course" => "Medicine",
"grade" => "Final year"),
);
// Printing all the keys and values one by one
$keys = array_keys($college);
for($i = 0; $i < count($college); $i++) {
echo $keys[$i] . ":<br>";
foreach($college[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "<br>";
}
?>
</body>
</html>
Output
Record of Conqueror
Department : Computer Sciences
Course : Software Engineering
Grade : First Class
Record of Performance
Conqueror:
department : Computer Sciences
Course : Software Engineering
grade : First Class
Favour:
department : Medical Sciences
Course : Medicine
grade : Final year
Divine:
department : Medical Sciences
Course : Medicine
grade : Final year