如何使用PHP在JSON数组中访问这些数据?
我正在尝试访问JSON数组中的某个数组,我似乎不知道如何访问此数据,请提供任何帮助 以下是JSON数组:如何使用PHP在JSON数组中访问这些数据?,php,arrays,json,Php,Arrays,Json,我正在尝试访问JSON数组中的某个数组,我似乎不知道如何访问此数据,请提供任何帮助 以下是JSON数组: 它没有唯一的标识符,这就是我在访问它时遇到问题的原因。您应该简单地使用json\u decode函数,然后您将拥有可以毫无问题地访问的PHP数组您应该简单地使用json\u decode函数,然后您将拥有可以毫无问题地访问的PHP数组使用json\u decode()。它将为您提供一个数组值。使用json\u decode()。它将为您提供一个数组值。假设您希望从这个json数据访问国家名称
它没有唯一的标识符,这就是我在访问它时遇到问题的原因。您应该简单地使用
json\u decode
函数,然后您将拥有可以毫无问题地访问的PHP数组您应该简单地使用json\u decode
函数,然后您将拥有可以毫无问题地访问的PHP数组使用json\u decode()
。它将为您提供一个数组值。使用json\u decode()
。它将为您提供一个数组值。假设您希望从这个json数据访问国家名称。您已经将json数据存储在$geocode变量中,然后可以执行以下操作。这将为您提供键名为country的键的精确值,无论它位于哪个索引上
$output=json_decode($geocode)
如果您确定索引值,您可以访问它,如下所示
$country=$output->results[0]->address\u components[5]->long\u name
希望获得以下帮助:)假设您希望从这个json数据访问国家名称。您已经将json数据存储在$geocode变量中,然后可以执行以下操作。这将为您提供键名为country的键的精确值,无论它位于哪个索引上 $output=json_decode($geocode) 如果您确定索引值,您可以访问它,如下所示 $country=$output->results[0]->address\u components[5]->long\u name
希望得到以下帮助:)首先检查中的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]);
?>
首先检查中的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]);
?>