如何在php中打印json文件数组中的数据
Posted
技术标签:
【中文标题】如何在php中打印json文件数组中的数据【英文标题】:How can i print the data in the array of json file in php 【发布时间】:2022-01-10 03:32:26 【问题描述】:我有一个我已经解码的 json 文件。现在我正在尝试打印数组中的每个元素,它们是“id、bin、tur、bank_name”等。我怎样才能到达例如数组的第二个元素并将其打印为表格。
$json_url = "websitename";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
我的输出是这样的;
数组
(
[0] => Array
(
[id] => 13355
[bin] => 540134
[tur] => mc
[banka_adi] => T.C.ZİRAAT BANKASI A.Ş.
[type] => maximum
[name] => ziraat
[created_at] => 2019-08-11 21:10:12
[updated_at] => 2019-08-11 21:10:12
[ekalan] =>
)
[1] => Array
(
[id] => 13356
[bin] => 547287
[tur] => mc
[banka_adi] => T.C.ZİRAAT BANKASI A.Ş.
[type] => maximum
[name] => ziraat
[created_at] => 2019-08-11 21:10:12
[updated_at] => 2019-08-11 21:10:12
[ekalan] =>
)
[2] => Array
(
[id] => 13357
[bin] => 542374
[tur] => mc
[banka_adi] => T.C.ZİRAAT BANKASI A.Ş.
[type] => maximum
[name] => ziraat
[created_at] => 2019-08-11 21:10:12
[updated_at] => 2019-08-11 21:10:12
[ekalan] =>
)
【问题讨论】:
【参考方案1】:您可以简单地迭代数组并显示它们:
$json_url = "websitename";
$json = file_get_contents($json_url);
$data = json_decode($json, true);
echo "<table>";
foreach($data as $record)
echo "<tr>";
echo "<td>" . $record["id"] . "/<td>";
echo "<td>" . $record["bin"] . "/<td>";
echo "<td>" . $record["tur"] . "/<td>";
echo "</tr>";
echo "</table>";
更改$record
键以添加或删除您需要显示的元素。
【讨论】:
以上是关于如何在php中打印json文件数组中的数据的主要内容,如果未能解决你的问题,请参考以下文章
PHP如何把数据写入JSON文件并在另一PHP文件读取JSON数据?