如果我有下面的JSON,當ID == 6
時,如何得到Name
和Age
的值?從特定條件中選擇JSON
[{"Name":" Jim", "ID":"6", "Age": "0"},{"Name":" Bob", "ID":"53", "Age": "0"}]
我試圖做到這一點,到目前爲止,但我得到了以下錯誤:
Notice: Trying to get property of non-object on line 3
$json = '[{"Name":" Jim", "ID":"6", "Age": "0"},{"Name":" Bob", "ID":"53", "Age": "0"}]';
$json2 = json_decode($json);
if($json2->ID == '6') {
echo $json2->Name;
echo $json2->Age;
}
你唯一缺少的就是foreach循環,在你試圖訪問每個對象的名字和年齡之前,循環遍歷所有對象。 – Rizier123