Php 如何访问JSON元素

Php 如何访问JSON元素,php,json,Php,Json,我有一个json,我想从那个json打印“格式化的地址”元素 Json { "html_attributions" : [], "results" : [ { "formatted_address" : "Narayan Peth, Pune, Maharashtra 411030, India", "geometry" : { "location" : { "lat" : 18.5

我有一个json,我想从那个json打印“格式化的地址”元素

Json

{
   "html_attributions" : [],
   "results" : [
      {
         "formatted_address" : "Narayan Peth, Pune, Maharashtra 411030, India",
         "geometry" : {
            "location" : {
               "lat" : 18.515797,
               "lng" : 73.852335
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
         "id" : "3a25975b3806df28aa79ac4a8d954c307be4aa57",
         "name" : "Aditya Medical",
         "place_id" : "ChIJJxwmOHDAwjsRjRDO4LnGJ-I",
         "reference" : "CmRSAAAA3E7ih55-2BZjRQcw_URQ2gwi8eWb5HU6hdfNUj_TqtDJ7TtASVMowcuWMkohNjp7F6UKuGsMuR-IlzZEt4YUJyzNxzWg-TYy6hyN8P5n2asAO6ztZeU3oHZdH7OBFFW_EhBe4cQbAU99oILcDmvv_gOhGhR7jzP0Z9-mDrncd5Gr9hOY7aOqRg",
         "types" : [ "pharmacy", "health", "store", "point_of_interest", "establishment" ]
      }
   ],
   "status" : "OK"
}
我厌倦了使用打印,但无法打印

foreach (json_decode($address[0]->Response) as $obj){
     print_r($obj['results']['formatted_address']);
}
试一试

具有第二个参数来确定返回的结果格式-
对象
数组

尝试


具有第二个参数来确定返回的结果格式-
对象
数组

您需要将第二个参数设置为true才能将json设置为数组。另外,您的
格式化的\u地址也在数组中,因此需要在其中传递索引

foreach (json_decode($address[0]->Response, true) as $obj){
     print_r($obj['results'][0]['formatted_address']);
}

您需要将第二个参数设置为true以获取json作为数组。另外,您的
格式化的\u地址也在数组中,因此需要在其中传递索引

foreach (json_decode($address[0]->Response, true) as $obj){
     print_r($obj['results'][0]['formatted_address']);
}

这可能是一个解决方案:

$jsonAsArray = json_decode($yourJson, true);
$results = $array["results"][0];

var_dump($results['formatted_address']);

祝你好运,这可能是一个解决方案:

$jsonAsArray = json_decode($yourJson, true);
$results = $array["results"][0];

var_dump($results['formatted_address']);

祝你好运

我试着像@Plamen Nikolov一样,但不起作用。我试着把“真”改为1,这就是工作

foreach (json_decode($address[0]->Response, 1) as $obj){
     print_r($obj['results']['formatted_address']);
}

我试着像@Plamen Nikolov一样,但不起作用。我试着把“真”改为1,这就是工作

foreach (json_decode($address[0]->Response, 1) as $obj){
     print_r($obj['results']['formatted_address']);
}
试一试

foreach (json_decode($address[0]->Response)->results as $obj){
     print_r($obj->formatted_address);
}
json_decode($address[0]->Response)将为您提供一个对象。不应使用like数组。所以您应该使用“->”而不是数组形式。更好的是,您可以将结果放在foreach上,并从$obj获取格式化的_地址的数据

--试试看

foreach (json_decode($address[0]->Response)->results as $obj){
     print_r($obj->formatted_address);
}
json_decode($address[0]->Response)将为您提供一个对象。不应使用like数组。所以您应该使用“->”而不是数组形式。更好的是,您可以将结果放在foreach上,并从$obj获取格式化的_地址的数据


--

首先尝试打印
$obj
它们是对象,不是数组。将
true
作为
json_decode
中的第二个参数传递以获取数组,或者将对象用作对象。首先尝试打印
$obj
它们是对象,而不是数组。将
true
作为
json_decode
中的第二个参数传递以获取数组,或者将对象用作对象。获取通知:尝试获取非对象的属性设置通知:尝试获取