我有一个JSON字符串,我已经使用“json_decode”转换为关联的PHP数组。出于某种原因,我似乎无法弄清楚如何在数组中找到我正在寻找的值的索引路径。
这是JSON字符串:
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "175 Hemenway St, Boston, MA 02115, USA",
"address_components": [ {
"long_name": "175",
"short_name": "175",
"types": [ "street_number" ]
}, {
"long_name": "Hemenway St",
"short_name": "Hemenway St",
"types": [ "route" ]
}, {
"long_name": "Boston",
"short_name": "Boston",
"types": [ "locality", "political" ]
}, {
"long_name": "Boston",
"short_name": "Boston",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "Suffolk",
"short_name": "Suffolk",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "Massachusetts",
"short_name": "MA",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "02115",
"short_name": "02115",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 42.3411740,
"lng": -71.0912860
},
"location_type": "ROOFTOP",
"viewport": {
"southwest": {
"lat": 42.3380264,
"lng": -71.0944336
},
"northeast": {
"lat": 42.3443216,
"lng": -71.0881384
}
}
}
} ]
}
这是PHP中关联数组的var_dump输出
array(2) { ["status"]=> string(2) "OK" ["results"]=> array(1) { [0]=> array(4) { ["types"]=> array(1) { [0]=> string(14) "street_address" } ["formatted_address"]=> string(38) "175 Hemenway St, Boston, MA 02115, USA" ["address_components"]=> array(8) { [0]=> array(3) { ["long_name"]=> string(3) "175" ["short_name"]=> string(3) "175" ["types"]=> array(1) { [0]=> string(13) "street_number" } } [1]=> array(3) { ["long_name"]=> string(11) "Hemenway St" ["short_name"]=> string(11) "Hemenway St" ["types"]=> array(1) { [0]=> string(5) "route" } } [2]=> array(3) { ["long_name"]=> string(6) "Boston" ["short_name"]=> string(6) "Boston" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [3]=> array(3) { ["long_name"]=> string(6) "Boston" ["short_name"]=> string(6) "Boston" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_3" [1]=> string(9) "political" } } [4]=> array(3) { ["long_name"]=> string(7) "Suffolk" ["short_name"]=> string(7) "Suffolk" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [5]=> array(3) { ["long_name"]=> string(13) "Massachusetts" ["short_name"]=> string(2) "MA" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [6]=> array(3) { ["long_name"]=> string(13) "United States" ["short_name"]=> string(2) "US" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [7]=> array(3) { ["long_name"]=> string(5) "02115" ["short_name"]=> string(5) "02115" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["geometry"]=> array(3) { ["location"]=> array(2) { ["lat"]=> float(42.341174) ["lng"]=> float(-71.091286) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> array(2) { ["southwest"]=> array(2) { ["lat"]=> float(42.3380264) ["lng"]=> float(-71.0944336) } ["northeast"]=> array(2) { ["lat"]=> float(42.3443216) ["lng"]=> float(-71.0881384) } } } } } }
理想情况下,我希望索引“location”下的数组(lat& lng键下的值)
我似乎没有比array_name [“results”]
更进一步是否有替代var_dump的方法可以对多级数组进行行分隔和缩进,以便我能理解它们?我宁愿使用直接路径而不是递归搜索整个数组来找到我正在寻找的东西。谢谢!
答案 0 :(得分:3)
您是否尝试以这种方式访问“位置”?:
$location = $array_name['results'][0]['geometry']['location'];
$lat = $location['lat'];
$lng = $location['lng'];
答案 1 :(得分:2)
是否有替代var_dump的方法可以对多级数组进行行分隔和缩进,以便我能理解它们?
print_r,var dump和var将所有输出导出为单独的多行文本。
http://ca2.php.net/print_r
http://ca2.php.net/manual/en/function.var-dump.php
http://ca2.php.net/manual/en/function.var-export.php
您只是没有看到它,因为您正在查看HTML文档的渲染结果,而不是原始文本。查看来源,或使用<br/>
HTML元素替换新行,或将输出括在<pre>
标记中。
请记住,浏览器在渲染时往往会忽略空行,例如新行和缩进。