How can a multidimensional array be displayed in a table in PHP without overwriting values?
When displaying a multidimensional array in a table in PHP, you need to loop through each row and column of the array to populate the table cells. To avoid overwriting values, you can use nested loops to iterate through the array and display the values in the corresponding table cells.
<?php
// Sample multidimensional array
$array = array(
array("John", "Doe", 30),
array("Jane", "Smith", 25),
array("Tom", "Brown", 35)
);
echo "<table border='1'>";
foreach ($array as $row) {
echo "<tr>";
foreach ($row as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Related Questions
- What potential issue or error could arise when using the PHP code to display detailed data from a database record?
- What are the security implications of automating form submissions on external websites using PHP?
- What are some common mistakes or misunderstandings when trying to copy folders in PHP, and how can they be avoided?