1

I'm trying to get to a certain value using foreach (which I believe is the best way, performance-wise, as of know of)

[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. 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.

Can someone help me do a foreach on this?

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!

hellomello
  • 8,219
  • 39
  • 151
  • 297
  • Possible duplicate of [stdClass object and foreach loops](http://stackoverflow.com/questions/950827/stdclass-object-and-foreach-loops) – Cullub Feb 08 '16 at 21:44

3 Answers3

2

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;
    }
}
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • shouldnt it be $business->location->coordinate->latitude, because coordinate is a member of the location object? – ma cılay Mar 21 '12 at 03:30
1

Try this:

foreach ($response->businesses as $business) {
    $selected_lat = $business->location->coordinate->latitude;
    $selected_lng = $business->location->coordinate->longitude;
}
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
0

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.

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.

Jason Larke
  • 5,289
  • 25
  • 28