What are some best practices for outputting a multidimensional array in a table format using PHP?
When outputting a multidimensional array in a table format using PHP, it is important to loop through the array to access each element and display it in a tabular format. One common approach is to use nested loops to iterate over the rows and columns of the array and output the data within HTML table tags.
<?php
// Sample multidimensional array
$data = array(
array('Name' => 'John', 'Age' => 25, 'City' => 'New York'),
array('Name' => 'Alice', 'Age' => 30, 'City' => 'Los Angeles'),
array('Name' => 'Bob', 'Age' => 22, 'City' => 'Chicago')
);
// Output array data in table format
echo '<table border="1">';
echo '<tr><th>Name</th><th>Age</th><th>City</th></tr>';
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
Related questions
-
MYSQL PDO database operations
-
MySQL database queries timestamp
-
SQL injection PDO prepared statements