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

时间:2018-05-17 07:59:56

标签: php json codeigniter

我正在使用Google Map Api,并以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是上面提到的输出结果,通过使用上面的代码并且它可以工作但有时我得到未定义的错误的错误:0,在$ lat和$ lng行,有没有其他方法来检索来自JSON Code的具体数据,我已在互联网上搜索但我不知道如何解决这个问题,请帮帮我

3 个答案:

答案 0 :(得分:0)

您可以编写如下代码:

@Service
public class CommonUtilServiceImpl implements CommonUtilService {

它会起作用。

答案 1 :(得分:0)

$url='your url';            
$array = file_get_contents($url);
$array = (array)json_decode($array);

答案 2 :(得分:0)

你也可以写代码。

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