如何从PHP获取json元素?

时间:2016-12-06 21:49:29

标签: php json

例如:

 {
   "destination_addresses" : [ "Milano, Italia" ],
   "origin_addresses" : [ "Padova PD, Italia" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "246 km",
                  "value" : 246492
               },
               "duration" : {
                  "text" : "2 ore 36 min",
                  "value" : 9388
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

我想在这种情况下获得246公里的距离 谢谢你的回复..我不知道......

FROM:https://maps.googleapis.com/maps/api/distancematrix/json?origins=Padova&destinations=Milano&language=it-IT&key=API_KEY

2 个答案:

答案 0 :(得分:1)

如果您收到的那条JSON是唯一的数据,则可以使用json_decode()创建array()

$json = '{"destination_addresses":["Milano, Italia"],"origin_addresses":["Padova PD, Italia"],"rows":[{"elements":[{"distance":{"text":"246 km","value":246492},"duration":{"text":"2 ore 36 min","value":9388},"status":"OK"}]}],"status":"OK"}';

$jsonArray = json_decode($json, true); // get array of json

var_dump($jsonArray);

结果如下:

array(4) {
    ["destination_addresses"] => array(1) {
        [0] => string(14) "Milano, Italia"
    },
    ["origin_addresses"] => array(1) {
        [0] => string(17) "Padova PD, Italia"
    },
    ["rows"] => array(1) {
        [0] => array(1) {
            ["elements"] => array(1) {
                [0] => array(3) {
                    ["distance"] => array(2) {
                        ["text"] => string(6) "246 km",
                        ["value"] => int(246492)
                    },
                    ["duration"] => array(2) {
                        ["text"] => string(12) "2 ore 36 min",
                        ["value"] => int(9388)
                    },
                    ["status"] => string(2) "OK"
                }
            }

然后你可以这样做以获得实际距离:

print_r($jsonArray['rows']['0']['elements']['0']['distance']['text']);

// 246 km

注意['0']是有的,因为如果查看print_r($jsonArray);,您可以看到这些是转换后的JSON格式文本的一部分。

答案 1 :(得分:1)

您可以使用array将其转换为json_decode,如下所示:

<?php
$json = json_decode('{
   "destination_addresses" : [ "Milano, Italia" ],
   "origin_addresses" : [ "Padova PD, Italia" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "246 km",
                  "value" : 246492
               },
               "duration" : {
                  "text" : "2 ore 36 min",
                  "value" : 9388
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}',true);

var_dump($json);
?>

结果是:

array(4) {
  ["destination_addresses"]=>
  array(1) {
    [0]=>
    string(14) "Milano, Italia"
  }
  ["origin_addresses"]=>
  array(1) {
    [0]=>
    string(17) "Padova PD, Italia"
  }
  ["rows"]=>
  array(1) {
    [0]=>
    array(1) {
      ["elements"]=>
      array(1) {
        [0]=>
        array(3) {
          ["distance"]=>
          array(2) {
            ["text"]=>
            string(6) "246 km"
            ["value"]=>
            int(246492)
          }
          ["duration"]=>
          array(2) {
            ["text"]=>
            string(12) "2 ore 36 min"
            ["value"]=>
            int(9388)
          }
          ["status"]=>
          string(2) "OK"
        }
      }
    }
  }
  ["status"]=>
  string(2) "OK"
}

为了只获得km,您可以在以后执行此操作:

<?php
    echo $json['rows']['0']['elements']['0']['distance']['text'];
?>