PHP JSON 和数组

发布于 2024-12-11 15:30:29 字数 1194 浏览 0 评论 0原文

我查询了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风蛊 2024-12-18 15:30:29

这应该可以解决街道的问题:

$street['location'] = // array - it's not really clear in your code.   
echo '->latitude:' . $street['location']['latitude'] . '<br/>';
echo '-->street id:' . $street['location']['street']['id'] . '<br/>';
echo '-->street name:' . $street['location']['street']['name'] . '<br/>';
echo '->Longitude:' . $street['location']['longitude'] . '<br/>';

您不需要迭代提供数组来访问街道信息。


如果您想对存储在 $street['location']['street'] 中的数组使用 foreach:(

// ...
foreach($street['location']['street'] as $key => $value){
   echo '-->street ', $key, ': ', $value, '<br />';
}
// ...

请注意,您可以不需要使用. 当您只想回显某些内容时,只需使用,。)

This should solve the problem with the street:

$street['location'] = // array - it's not really clear in your code.   
echo '->latitude:' . $street['location']['latitude'] . '<br/>';
echo '-->street id:' . $street['location']['street']['id'] . '<br/>';
echo '-->street name:' . $street['location']['street']['name'] . '<br/>';
echo '->Longitude:' . $street['location']['longitude'] . '<br/>';

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']:

// ...
foreach($street['location']['street'] as $key => $value){
   echo '-->street ', $key, ': ', $value, '<br />';
}
// ...

(Note that you can don't need to concatenate using . when you just want to echo something and can just use the ,.)

混吃等死 2024-12-18 15:30:29

您转储了一个值 ($street['location']),但是 foreach 却在 $location['street'] 上循环。所以很可能你的 foreach 值被颠倒了。

如果要信任 var_dump,那么您甚至不需要 foreach 循环:

echo '->latitude:' . $location['latitude'] . '<br/>';
echo '-->street id:' . $location['street']['id'] . '<br/>';
echo '-->street name:' . $location['street']['name'] . '<br/>';

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:

echo '->latitude:' . $location['latitude'] . '<br/>';
echo '-->street id:' . $location['street']['id'] . '<br/>';
echo '-->street name:' . $location['street']['name'] . '<br/>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文