使用PHP和数组键中的空格来解析Json响应 [英] Parsing Json response using PHP with space in array key

查看:96
本文介绍了使用PHP和数组键中的空格来解析Json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个邮政编码API的回复.但是,由于两个词之间存在空格,我无法弄清楚地名"中的值.不太确定从这里去哪里.

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"
   }
 }
}

推荐答案

您需要将它们放在大括号内并用单引号引起来:

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

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

或者如@scragar在评论中所述,如果您不方便通过对象访问它们,可以在json_decode($response, true)上放置true标志,以便可以将它们作为关联数组访问.

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.

这篇关于使用PHP和数组键中的空格来解析Json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆