通过php将json嵌套到表中



我有一个json,想把它转换成一个包含json源中所有值的表。这是json:的一小部分

[sport_events] => Array
(
[0] => stdClass Object
(
[id] => sr:match:14972279
[scheduled] => 2018-10-11T17:00:00+00:00
[start_time_tbd] => 
[status] => not_started
[tournament_round] => stdClass Object
(
[type] => group
[number] => 1
[group] => A
)

我有这样的代码,它循环json,并很好地从所有sport_events:中获得一级值(即id、scheduled、start_time_tbd、status(

<?php
$json=file_get_contents("json_url");
$data =  json_decode($json);
if (count($data->sport_events)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->sport_events as $idx => $sport_events) {
// Output a row
echo "<tr>";
echo "<td>$sport_events->id</td>";
echo "<td>$sport_events->scheduled</td>";
echo "<td>$sport_events->start_time_tbd</td>";
echo "<td>$sport_events->status</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>

我如何更改代码以获得嵌套值(即锦标赛中的类型、数字和组(?

谢谢!

<?php
$json=file_get_contents("json_url");
$data =  json_decode($json);
if (count($data->sport_events)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->sport_events as $idx => $sport_events) {
// Output a row
echo "<tr>";
echo "<td>$sport_events->id</td>";
echo "<td>$sport_events->scheduled</td>";
echo "<td>$sport_events->start_time_tbd</td>";
echo "<td>$sport_events->status</td>";
echo "</tr>";
foreach($sport_events->tournament_round as $tournament) {
echo $tournament->type;
echo $tournament->number;
echo $tournament->group;
}
}
// Close the table
echo "</table>";
}

最新更新