0

I have a case where I may recive one of two json objects, in this case from either the google geocode api, or the places api.

Acccessing values from the geocoding api it will look like this:

$coordinates            = $data->results[0]->geometry->location;
$cache_value['lat'] = (string) $coordinates->lat;
$cache_value['lng'] = (string) $coordinates->lng;

and an almost identical structure for places.

$coordinates            = $data->result->geometry->location;
$cache_value['lat'] = (string) $coordinates->lat;
$cache_value['lng'] = (string) $coordinates->lng;

In my code I've two functions to handle each case, but they are almost idential with the exception of the result vs results[0] and I'd like to combine them. I've tried to passing a varriable but it throws errors:

$result         = ($place) ? 'result' : 'results[0]';
$coordinates    = $data->$result->geometry->location;

Gives the following:

Notice: Undefined property: stdClass::$result[0]

I'd like to know the correct syntax to achive what im after, and any pointers on nominclature, as Im afraid this question title is a bit inpercise.

5
  • Is that missing quote on 'results[0]; a typo? Commented Sep 30, 2013 at 15:24
  • 1
    why not just $result = ($place) ? $data->result : $data->results[0]; and then $coordinates = $result->geometry->location; Commented Sep 30, 2013 at 15:27
  • @RUJordan, yup that's been corrected now - thanks Commented Sep 30, 2013 at 15:38
  • @stakolee - thats a good point, I'll chew on that one. Commented Sep 30, 2013 at 15:44
  • 1
    @stakolee - btw, nice missippi john hurt / SO conflation there! Commented Sep 30, 2013 at 15:44

3 Answers 3

1

Just do:

$result         = $place ? $data->result : $data->results[0];
$coordinates    = $result->geometry->location;

What your code is doing, is this: It tries to resolve a property of $data object with a name of results[0], and there is none; Once again - it does not resolve 0 index of the results property, but it tries to find a property with a literal name results[0]; It would work if your object looked like this:

$obj = (object)array( 'results[0]' => 'hey there' );

If for any reason you would like to play with that, you could create a silly property like this: $data->{'results[0]'} = 5; - but it's stupid, don't do that :)

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the extra explanation as to what is going on.
0

i believe php is looking for a key named results[0], its not smart enough to know that the properties name is results and you want the first member of the collection [0]

Comments

0

The issue is the reference of the variable name, rather than its value.

$result = ($place) ? 'result' : 'results[0]';
$coordinates = $data->$result->geometry->location; 

$result is just a string and should be the actual value of either $data->result or $data->result[0].

To correct it, simply use $result to hold the value of result.

$result = ($place) ? $data->result : $data->results[0];
$coordinates = $result->geometry->location; 

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.