4

I have a response for a zip code API. However, I cannot figure out how to the the value from 'place name' because of the space between the two words. No quite sure where to go from here.

object(stdClass)#1 (4) {
 ["post code"]=>
 string(5) "42223"
 ["country"]=>
 string(13) "United States"
 ["country abbreviation"]=>
 string(2) "US"
 ["places"]=>
 array(1) {
   [0]=>
   object(stdClass)#2 (5) {
     ["place name"]=>
     string(13) "Fort Campbell"
     ["longitude"]=>
     string(8) "-87.5585"
     ["state"]=>
     string(8) "Kentucky"
     ["state abbreviation"]=>
     string(2) "KY"
     ["latitude"]=>
     string(7) "36.5995"
   }
 }
}
1
  • 2
    have you considered using $array_output = json_decode($str_api_response, TRUE); to get it as an array then you can quote your column access: echo $array_output['places'][0]['place name']; Commented Sep 23, 2014 at 15:55

1 Answer 1

22

You need to put them inside a curly brace with a single quote:

$place_name = $response->places[0]->{'place name'};
echo $place_name;

Or as @scragar said in the comments, if you're not confortable accessing them thru objects, you can put a true flag on json_decode($response, true), so that you can access them as associative arrays instead.

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

1 Comment

Thank you! So simple but I couldn't figure it out. Worked great! When it lets me I'll mark this as the correct answer.

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.