How can PHP beginners effectively format JSON arrays into tables for display in forums?
To effectively format JSON arrays into tables for display in forums, PHP beginners can use the json_decode function to convert the JSON data into an associative array. Then, they can loop through the array to extract the data and display it in a table format using HTML.
<?php
$json_data = '[{"name":"John Doe","age":30,"city":"New York"},{"name":"Jane Smith","age":25,"city":"Los Angeles"}]';
$data = json_decode($json_data, true);
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>';
?>