Php json文件获取内容并显示项目
我有这段代码,但我无法使项目正确显示,我只能显示来自echo$data语句的数据,而不能显示$item FirstName或item BioPhp json文件获取内容并显示项目,php,json,Php,Json,我有这段代码,但我无法使项目正确显示,我只能显示来自echo$data语句的数据,而不能显示$item FirstName或item Bio $url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965'; // path to your JSON file $data = file_get_contents($url); // put the contents of the file into a variable $items
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$items = json_decode($data, true);
foreach ($items as $item)
{
$name = $item = ["FirstName"];
$bio = $item = ["Bio"];
}
echo $data
如果您看到json响应,它有2个元素
{
消息:…
,部门:…
}
因此,您必须循环抛出
$items['department']
,而不是$items
您还可以访问数据,如$item['Bio']
因此,您可以像这样更改代码以获取信息
<?php
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965';
$data = file_get_contents($url); // into a variable
$items = json_decode($data, true); // decode the JSON feed
foreach ($items['department'] as $item)
{
$name = $item["FirstName"];
$bio = $item["Bio"];
echo $name . ' ' . $bio . PHP_EOL;
}
请阅读php基础知识<代码>$name=$item=[“FirstName”]代码>与您期望的不同。$name=$item[“FirstName”]$bio=$item[“bio”];