PHP JSON 和数组
我查询了一个 API,但在显示返回的代码 (JSON) 时遇到了一些麻烦。如果我执行 var_dump,我会得到这样的结果:
var_dump($street['location']);
array(3) {
["latitude"]=> string(10) "52.6397278"
["street"]=> array(2) {
["id"]=> int(469)
["name"]=> string(23) "On or near Abbey Street"
}
["longitude"]=> string(10) "-1.1322920"
}
现在,通常,我会执行类似的操作来显示它:
var_dump($street['location']);
echo '->latitude:' . $location['latitude'] . '<br/>';
foreach($location['street'] as $st){
echo '-->street id:' . $st['id'] . '<br/>';
echo '-->street name:' . $st['name'] . '<br/>';
}
echo '->Longitude:' . $location['longitude'] . '<br/>';
但我得到:
array(3) {
["latitude"]=> string(10) "52.6397278"
["street"]=> array(2) {
["id"]=> int(469)
["name"]=> string(23) "On or near Abbey Street"
}
["longitude"]=> string(10) "-1.1322920"
} ->latitude:5
Warning: Invalid argument supplied for foreach() in /home/pasd529/public_html/npia.php on line 102
->Longitude:5
纬度/经度被截断,我无法获取 streed id / name .. 。
感谢您的帮助
I've querying an API and I've got some troubles to display the code returned (JSON). If I do a var_dump, I have something like this:
var_dump($street['location']);
array(3) {
["latitude"]=> string(10) "52.6397278"
["street"]=> array(2) {
["id"]=> int(469)
["name"]=> string(23) "On or near Abbey Street"
}
["longitude"]=> string(10) "-1.1322920"
}
Now, usually, I will do something like that to have it displayed:
var_dump($street['location']);
echo '->latitude:' . $location['latitude'] . '<br/>';
foreach($location['street'] as $st){
echo '-->street id:' . $st['id'] . '<br/>';
echo '-->street name:' . $st['name'] . '<br/>';
}
echo '->Longitude:' . $location['longitude'] . '<br/>';
But I get:
array(3) {
["latitude"]=> string(10) "52.6397278"
["street"]=> array(2) {
["id"]=> int(469)
["name"]=> string(23) "On or near Abbey Street"
}
["longitude"]=> string(10) "-1.1322920"
} ->latitude:5
Warning: Invalid argument supplied for foreach() in /home/pasd529/public_html/npia.php on line 102
->Longitude:5
The latitude / longitude are truncated and I can't get the streed id / name ...
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以解决街道的问题:
您不需要迭代提供数组来访问街道信息。
如果您想对存储在
$street['location']['street']
中的数组使用foreach
:(请注意,您可以不需要使用
.
当您只想回显
某些内容时,只需使用,
。)This should solve the problem with the street:
You do not need to iterate ofer the array to access the street information.
If you want to use
foreach
for the array stored in$street['location']['street']
:(Note that you can don't need to concatenate using
.
when you just want toecho
something and can just use the,
.)您转储了一个值 (
$street['location'])
,但是 foreach 却在$location['street']
上循环。所以很可能你的 foreach 值被颠倒了。如果要信任 var_dump,那么您甚至不需要 foreach 循环:
You're dumping out one value (
$street['location'])
, but are foreach looping on$location['street']
instead. So most likely you've got your foreach value reversed.If the var_dump is to be trusted, then you don't even need the foreach loop: