2017-06-14 46 views
0

我有這樣的代碼,但我不能讓這些項正常顯示,我只能說明沒有回聲$數據語句的數據,但不能$項姓或項目生物JSON的file_get_contents和表演項目

$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 
+2

請閱讀php基礎知識。 '$ name = $ item = [「FirstName」];'不符合你的期望。 –

+0

$ name = $ item [「FirstName」]; $ bio = $ item [「Bio」]; –

回答

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; 
    } 
+0

非常感謝你!工作得很好 – Mariano