我正在尝试访问JSON数组中的某个数组,我似乎无法弄清楚如何访问这些数据,任何帮助都将不胜感激!
这是JSON数组:https://gist.github.com/anonymous/a37852a7436d31289390
它没有唯一标识符,这就是我无法访问它的原因。
答案 0 :(得分:2)
您应该简单地使用json_decode
函数,然后您将拥有可以无问题地访问的PHP数组
答案 1 :(得分:2)
假设您要从此json数据中访问国家/地区名称。并且您已将该json数据存储在 $ geocode 变量中,然后您可以执行以下操作。这将为您提供密钥名称为country的密钥的确切值,无论它在哪个索引上。
$ output = json_decode($ geocode);
foreach($output->results[0]->address_components as $key=>$val)
{
if($val->types[0]=='country' || $val->types[1]=='country')
{
$country=$output->results[0]->address_components[$key]->long_name;
}
}
如果您确定索引值,可以按以下方式访问
$ country = $ output-> results [0] - > address_components [5] - > long_name;
希望这有帮助:)
答案 2 :(得分:0)
使用json_decode()
。它会给你一个数组值。
答案 3 :(得分:0)
首先检查 http://jsonlint.com 中的json字符串,这是无效的json。
检查以下示例:
<?php
$jsonString = '{
"results": [
{
"address_components": [
{
"long_name": "Snip",
"short_name": "Snip",
"types": [
"street_number"
]
},
{
"long_name": "Snip",
"short_name": "Snip",
"types": [
"route"
]
},
{
"long_name": "Desert Ridge",
"short_name": "Desert Ridge",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Snip",
"short_name": "Snip",
"types": [
"locality",
"political"
]
},
{
"long_name": "Snip",
"short_name": "Snip",
"types": [
"administrative_area_level_3",
"political"
]
}
]
}
]
}';
// Converting Json String to php jsonArray
$jsonArray = json_decode($jsonString);
// Initially its started with an object so you can't access like an array
//echo '<pre>';
//print_r($jsonArray);
// If you want to use results array check the following code
$results = $jsonArray->results;
echo '<pre>';
print_r($results);
/*
* If you want to access address_components key in first set of
* results then you need to use $resluts[0]->address_components[0]
* because every array have multiple objects, check the jsonArray
* once before accessing it wheather it is an array or object.
*/
print_r($results[0]->address_components[0]);
?>