Monday, November 14, 2022

PHP Multidimensional array and printing on table format

 PHP Multidimensional array and printing on table format





// Name physics chemistry math
// Anil 75 78 80
// Vijay 76 47 56
// Ramesh 85 45 96

$marks = [
"Anil" => ["Physics" => 75, "Chemistry" => 78, "Mathematics" => 80],
"Vijay" => ["Physics" => 76, "Chemistry" => 47, "Mathematics" => 56],
"Ramesh" => ["Physics" => 85, "Chemistry" => 45, "Mathematics" => 96],
];
// Using foreach loop
echo "<table border='2px', cellpadding='5px' cellspacing='0'>
<tr>
<td>Name</td>
<td>Physics</td>
<td>Chemistry</td>
<td>Mathematics</td>
</tr>
";
foreach ($marks as $key => $value1) {
echo "<tr><td>" . $key . "</td>";
foreach ($value1 as $value2) {
echo "<td>" . $value2 . "</td> ";
}
echo "</tr>";
}
echo "</table>";

0 comments

Post a Comment