Json数据(pincodes.json):
[{
"officename": "Netajinagar B.O",
"pincode": 744207,
"taluk": "Hut Bay",
"districtname": "South Andaman",
"statename": "ANDAMAN \u0026 NICOBAR ISLANDS"
}, {
"officename": "Tushnabad B.O",
"pincode": 744103,
"taluk": "Port Blair",
"districtname": "South Andaman",
"statename": "ANDAMAN \u0026 NICOBAR ISLANDS"
}, {
"officename": "Uttara B.O",
"pincode": 744209,
"taluk": "Rangat",
"districtname": "North And Middle Andaman",
"statename": "ANDAMAN \u0026 NICOBAR ISLANDS"
}]
PHP代码:
<?php
$string = file_get_contents("pincodes.json");
$json_s = json_decode($string);
foreach($json_s[0] as $item)
{
if($item->pincode == "686563")
{
echo $item->officename;
}else{
echo "Item not found";
}
}
我正在尝试根据条件(officename
)获取元素的特定值(pincode=686563
),但是当执行获取错误时
为foreach()提供的参数无效
答案 0 :(得分:4)