我有一些我正在尝试解码的 JSON——它正确地提取了值,但也产生了错误。
这是var_dump
:
["success"]=>
bool(true)
["providers"]=>
array(2) {
[0]=>
object(stdClass)#5 (15) {
["address1"]=>
string(14) "3240 W Lake St"
["address2"]=>
NULL
["city"]=>
string(11) "Minneapolis"
["crossStreet"]=>
string(26) "Lake Street & Market Plaza"
["description"]=>
string(55) "test location28402 description of services/prices/hours"
["distance"]=>
float(0.42900015862223)
["lat"]=>
float(44.948469)
["lon"]=>
float(-93.321155)
["name"]=>
string(17) "testlocation28402"
["phone"]=>
string(10) "6125551212"
["precise"]=>
bool(true)
["state"]=>
string(2) "MN"
["url"]=>
string(41) "http://www.testlocation28402.com?id=28402"
["urlCaption"]=>
string(25) "www.testlocation28402.com"
["zip"]=>
string(9) "554164512"
}
[1]=>
object(stdClass)#6 (15) {
["address1"]=>
string(19) "4335 Excelsior Blvd"
["address2"]=>
NULL
["city"]=>
string(16) "Saint Louis Park"
["crossStreet"]=>
NULL
["description"]=>
string(55) "test location26358 description of services/prices/hours"
["distance"]=>
float(0.91979730006713)
["lat"]=>
float(44.935773)
["lon"]=>
float(-93.33489)
["name"]=>
string(17) "testlocation26358"
["phone"]=>
string(10) "6125551212"
["precise"]=>
bool(true)
["state"]=>
string(2) "MN"
["url"]=>
string(41) "http://www.testlocation26358.com?id=26358"
["urlCaption"]=>
string(25) "www.testlocation26358.com"
["zip"]=>
string(9) "554164811"
}
}
["errors"]=>array(0) {
}
}
以上是var_dump
变量$jso
n的。我正在尝试使用以下方法提取 CITY 字段:
$json = json_decode($surescript);
foreach ($json as $providers){
foreach($providers as $onespot) {
echo "*";
echo $onespot->city;
echo "\n";
}
}
这输出:
警告:在第153行的/home/content/96/7973196/html/channels/MIL432/storeFinder_2.php中为 foreach() 提供的参数无效
*Minneapolis *Saint Louis Park
因此,它得到了正确的嵌套变量,但也在第二个 foreach 循环中抛出了一个错误。
我做错了什么?
在此先感谢您的帮助。