[英]Can't display JSON nested arrays with HTML table and PHP
我正在尝试显示显示每个国家/地区最新疫苗接种数据的表格。 我好不容易拉出国家列表,但是无法显示最新数据嵌套的arrays,接种总数,每百人接种总数。
我想显示total_vaccinations
和total_vaccinations_per_hundred
的数组,其中包含 date 的最新date
值。
我愿意使用 jQuery 和 Javascript 来完成这项工作。
这是 JSON 数据的一小部分。 原始数据在这里: https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.json
Array
(
[0] => Array
(
[country] => Afghanistan
[iso_code] => AFG
[data] => Array
(
[0] => Array
(
[date] => 2021-02-22
[total_vaccinations] => 0
[people_vaccinated] => 0
[total_vaccinations_per_hundred] => 0
[people_vaccinated_per_hundred] => 0
)
[1] => Array
(
[date] => 2021-02-23
[daily_vaccinations] => 1367
[daily_vaccinations_per_million] => 35
)
[2] => Array
(
[date] => 2021-02-24
[daily_vaccinations] => 1367
[daily_vaccinations_per_million] => 35
)
[3] => Array
(
[date] => 2021-02-25
[daily_vaccinations] => 1367
[daily_vaccinations_per_million] => 35
)
[4] => Array
(
[date] => 2021-02-26
[daily_vaccinations] => 1367
[daily_vaccinations_per_million] => 35
)
[5] => Array
(
[date] => 2021-02-27
[daily_vaccinations] => 1367
[daily_vaccinations_per_million] => 35
)
[6] => Array
(
[date] => 2021-02-28
[total_vaccinations] => 8200
[people_vaccinated] => 8200
[daily_vaccinations] => 1367
[total_vaccinations_per_hundred] => 0.02
[people_vaccinated_per_hundred] => 0.02
[daily_vaccinations_per_million] => 35
)
[7] => Array
(
[date] => 2021-03-01
[daily_vaccinations] => 1580
[daily_vaccinations_per_million] => 41
)
[8] => Array
(
[date] => 2021-03-02
[daily_vaccinations] => 1794
[daily_vaccinations_per_million] => 46
)
[9] => Array
(
[date] => 2021-03-03
[daily_vaccinations] => 2008
[daily_vaccinations_per_million] => 52
)
[10] => Array
(
[date] => 2021-03-04
[daily_vaccinations] => 2221
[daily_vaccinations_per_million] => 57
)
[11] => Array
(
[date] => 2021-03-05
[daily_vaccinations] => 2435
[daily_vaccinations_per_million] => 63
)
[12] => Array
(
[date] => 2021-03-06
[daily_vaccinations] => 2649
[daily_vaccinations_per_million] => 68
)
[13] => Array
(
[date] => 2021-03-07
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
[14] => Array
(
[date] => 2021-03-08
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
[15] => Array
(
[date] => 2021-03-09
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
[16] => Array
(
[date] => 2021-03-10
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
[17] => Array
(
[date] => 2021-03-11
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
[18] => Array
(
[date] => 2021-03-12
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
[19] => Array
(
[date] => 2021-03-13
[daily_vaccinations] => 2862
[daily_vaccinations_per_million] => 74
)
HTML/PHP 表格的小片段
<div class="container my-3 text-center">
<h2 class="display-6">Vaccine Live Data</h2>
<table class="table sortable" id="table" data-toggle="table">
<thead class="table-dark">
<tr>
<th scope="col">Countries</th>
<th scope="col">Last Updated</th>
<th scope="col">Total vaccinated</th>
<th scope="col">Total Vaccinated Per Hundred</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $key => $info){
?>
<tr>
<th class="sorttable_nosort">
<?php echo $info['country']; ?>
</th>
<td data-order='desc' data-field="firstdose" data-sortable="true">
<?php foreach($info['data'] as $key => $value) { echo $value['date'];
} ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
当前表output:图片在这里
试试这样的,还没完。 但你可以考虑一下。 基本上这个脚本获取国家的最后一个数组并显示它,假设这是最后更新的。 你可以用js表排序。
<div class="container my-3 text-center">
<h2 class="display-6">Vaccine Live Data</h2>
<table class="table sortable" id="table" data-toggle="table">
<thead class="table-dark">
<tr>
<th scope="col">Countries</th>
<th scope="col">Last Updated</th>
<th scope="col">Total vaccinated</th>
<th scope="col">Total Vaccinated Per Hundred</th>
<th scope="col">People Vaccinated Per Hundred</th>
</tr>
</thead>
<tbody>
<?php foreach($data as $key => $info){
$last_array = end($info["data"]);
?>
<tr>
<td class="sorttable_nosort">
<?php echo $info['country']; ?>
</td>
<td data-order='desc' data-field="firstdose" data-sortable="true">
<?=$last_array["date"]?>
</td>
<td data-order='desc' data-field="firstdose" data-sortable="true">
<?=$last_array["total_vaccinations"]?>
</td>
<td data-order='desc' data-field="firstdose" data-sortable="true">
<?=$last_array["total_vaccinations_per_hundred"]?>
</td>
<td data-order='desc' data-field="firstdose" data-sortable="true">
<?php
if($last_array["people_vaccinated_per_hundred"]){
echo $last_array["people_vaccinated_per_hundred"];
}else{
echo 'No data available';
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.