json file_get_contents和show item

时间:2017-06-14 19:21:31

标签: 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 = json_decode($data, true); 

foreach ($items as $item)
{
$name = $item = ["FirstName"];
$bio = $item = ["Bio"];
}

echo $data

1 个答案:

答案 0 :(得分:0)

如果你看到json响应它有2个元素。
    {
       message : ...
       department : ....
    }

所以你必须循环抛出$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); 
   foreach ($items['department'] as $item)
   {
       $name = $item["FirstName"];
       $bio = $item["Bio"];
       echo $name . ' ' . $bio . PHP_EOL;
   }