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!

1

3 Answers 3

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

shouldnt it be $business->location->coordinate->latitude, because coordinate is a member of the location object?
1

Try this:

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.