How can JSON data be displayed in a table using PHP?
To display JSON data in a table using PHP, you can first decode the JSON string using `json_decode()`, loop through the data, and output it in an HTML table format.
<?php
$json_data = '[{"id":1,"name":"John","age":30},{"id":2,"name":"Jane","age":25},{"id":3,"name":"Bob","age":35}]';
$data = json_decode($json_data, true);
echo '<table border="1">';
echo '<tr><th>ID</th><th>Name</th><th>Age</th></tr>';
foreach ($data as $row) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['age'] . '</td>';
echo '</tr>';
}
echo '</table>';
?>
Related Questions
- How can one troubleshoot and resolve MySQL connection problems in a PHP script on a local server?
- What are the potential pitfalls of using integer timestamps in a database instead of datetime data types for date sorting in PHP?
- What are some best practices for validating form input in PHP to prevent errors and ensure data integrity?