简体   繁体   English

Foreach循环数组和stdclass对象,获取值

[英]Foreach loop array and stdclass object, getting values

I'm trying to get to a certain value using foreach (which I believe is the best way, performance-wise, as of know of) 我试图使用foreach达到一定的价值(我认为这是最好的方式,性能方面,知道的)

[businesses] => Array
        (
            [0] => stdClass Object
                (
                    [rating] => 4
                    [location] => stdClass Object
                        (
                            [city] => Claremont
                            [display_address] => Array
                                (
                                    [0] => 580 W First St
                                    [1] => Claremont, CA 91711
                                )

                            [geo_accuracy] => 8
                            [postal_code] => 91711
                            [country_code] => US
                            [address] => Array
                                (
                                    [0] => 580 W First St
                                )

                            [coordinate] => stdClass Object
                                (
                                    [latitude] => 34.094112
                                    [longitude] => -117.7250746
                                )
                        )

                )
)

I'm trying to get latitude and longitude. 我想要获得经度和经度。 But keep in mind that I will have more than just [0] => stdClass Object . 但请记住,我将拥有的不仅仅是[0] => stdClass Object There will be multiple numbers. 会有多个号码。 I know I can do something like $response->businesses[0]->location or some sort, but that only gets the 0 key, I need to be able to foreach the keys to get it. 我知道我可以做一些像$ response-> BUSINESS [0] - > location或某种类型的东西,但是只获得0键,我需要能够通过密钥来获取它。

Can someone help me do a foreach on this? 有人可以帮我做一个关于这个的foreach吗?

for example I'm doing this so far... 例如我到目前为止这样做......

foreach($response->businesses->location as $llk=>$coordinate){

    if($llk === "coordinate"){

        $selected_lat = $coordinate->latitude;
        $selected_lng = $coordinate->longitude;
    }
}

So far its giving me errors. 到目前为止它给了我错误。

Thanks! 谢谢!

The following might be just what you are looking for: 以下可能正是您所寻找的:

foreach($response->businesses as $business) {
    if(!empty($business->location->coordinate)) {
       $coord = $business->location->coordinate;
       $selected_lat = $coord->latitude;
       $selected_long = $coord->longitude;
    }
}

Try this: 尝试这个:

foreach ($response->businesses as $business) {
    $selected_lat = $business->location->coordinate->latitude;
    $selected_lng = $business->location->coordinate->longitude;
}

You probably don't need a foreach loop for this, just use simple array iteration to go through the top level stdClass objects, then just use dereferencing to get each longitude/latitude pair. 你可能不需要foreach循环,只需使用简单的数组迭代来遍历顶级stdClass对象,然后使用解除引用来获取每个经度/纬度对。

for($i = 0; $i < count($response->businesses); $i++)
{
    $coords = $response->businesses[$i]->location->coordinate;
    $long = $coords->longitude;
    $lat = $coords->latitude;
}

That should work as far as I can tell. 就我所知,这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM