如何在Codeigniter Php中获取JSON的字段特定值?

如何在Codeigniter Php中获取JSON的字段特定值?,php,json,codeigniter,Php,Json,Codeigniter,我使用的是GoogleMapAPI,得到的结果是JSON格式的,输出结果是 { "results" : [ { "address_components" : [ { "long_name" : "Plot No 1", "short_name" : "Plot No 1", "types" : [ "premise" ]

我使用的是GoogleMapAPI,得到的结果是JSON格式的,输出结果是

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Plot No 1",
               "short_name" : "Plot No 1",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "B Block Road",
               "short_name" : "B Block Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Rangpuri Extention B-Block Pocket-4",
               "short_name" : "Rangpuri Extention B-Block Pocket-4",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "New Delhi",
               "short_name" : "New Delhi",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "South West Delhi",
               "short_name" : "South West Delhi",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Delhi",
               "short_name" : "DL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "110037",
               "short_name" : "110037",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Plot No 1, B Block Rd, Rangpuri Extention B-Block Pocket-4, New Delhi, Delhi 110037, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 28.5379687,
                  "lng" : 77.121816
               },
               "southwest" : {
                  "lat" : 28.5377897,
                  "lng" : 77.1217006
               }
            },
            "location" : {
               "lat" : 28.5378435,
               "lng" : 77.1217523
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 28.5392281802915,
                  "lng" : 77.12310728029151
               },
               "southwest" : {
                  "lat" : 28.5365302197085,
                  "lng" : 77.12040931970849
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJibjV7RYcDTkRnaFq2Lg__b4",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
}
我想提取lat和lng,在location对象下,写下我现在使用的代码

$mapcontent = json_decode($content,true);
 $lat = $mapcontent['results'][0]['geometry']['location']['lat'];
 $lng = $mapcontent['results'][0]['geometry']['location']['lng'];

其中$content是上面提到的输出结果,通过使用上面的代码,它可以工作,但有时我会得到未定义的offest:0错误,在$lat和$lng行上,是否有其他方法从JSON代码中检索特定数据,我已经在互联网上搜索了,但我不知道如何解决这个问题,请帮助我

您可以编写如下代码:

$mapcontent = json_decode($content,true);
$resultCount = count($mapcontent['results']);
if( $resultCount > 0 ) {
    $lat = $mapcontent['results'][0]['geometry']['location']['lat'];
    $lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}

它会起作用的

您还可以编写如下代码

$url='your url';            
$array = file_get_contents($url);
$array = (array)json_decode($array);
$mapcontent = json_decode($content,true);
if( true != empty($mapcontent['results'])) {
    $lat = $mapcontent['results'][0]['geometry']['location']['lat'];
    $lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}

然后首先检查索引我已经提到,上面的代码工作正常,但有时我会得到未定义偏移量的错误:$lat和$lng行上的0,我只是想用另一种方法从这个JSON检索数据,我已经提到,如果该索引是命中或未命中的,那么您可能应该首先检查索引0,通过
数组\u键\u存在
或可能
为空
。一个简单的三元运算符应该足以
count($mapcontent['results'][0])
仍在检查[0],如果找不到,将返回undefined。必须是
count($mapcontent['results'])是的@詹姆斯勒谢谢!!这只是因为复制和粘贴:)请尝试在答案中添加更多的描述或解释。代码的作用是什么?作者应该改变什么?它将如何解决这个问题?