What are some effective ways to organize and manipulate arrays in PHP to customize the output of data in an HTML table?
When working with arrays in PHP to customize the output of data in an HTML table, it is important to organize the data in a way that aligns with the desired table structure. One effective way to achieve this is by using loops to iterate through the array and manipulate the data accordingly. By using functions like `foreach` and `array_map`, you can easily customize the output of the data before displaying it in the HTML table.
<?php
// Sample array of data
$data = [
['name' => 'John Doe', 'age' => 30, 'city' => 'New York'],
['name' => 'Jane Smith', 'age' => 25, 'city' => 'Los Angeles'],
['name' => 'Bob Johnson', 'age' => 35, 'city' => 'Chicago']
];
// Output data in an HTML table
echo '<table>';
echo '<tr><th>Name</th><th>Age</th><th>City</th></tr>';
foreach ($data as $row) {
echo '<tr>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['age'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '</tr>';
}
echo '</table>';
?>