首页 > 解决方案 > 我有来自 API 的 json 数据,并想使用 PHP 从响应中提取数据

问题描述

我有来自以下格式的 API 的 json 数据。我想从中提取选定的数据,比如它的 id、代码、lat 和 lang。有人可以帮忙吗?我正在使用核心 PHP。

$countries_data = json_decode($countries, true);
print_r($countries_data);

Array
    (
        [data] => Array
            (
                [0] => Array
                    (
                        [id] => 241
                        [type] => country
                        [attributes] => Array
                            (
                                [code] => AF
                                [name] => Afghanistan
                                [lat] => 33.93911
                                [lng] => 67.709953
                            )

                    )

                [1] => Array
                    (
                        [id] => 235
                        [type] => country
                        [attributes] => Array
                            (
                                [code] => AL
                                [name] => Albania
                                [lat] => 41.153332
                                [lng] => 20.168331
                            )

                    )

                [2] => Array
                    (
                        [id] => 236
                        [type] => country
                        [attributes] => Array
                            (
                                [code] => DZ
                                [name] => Algeria
                                [lat] => 28.033886
                                [lng] => 1.659626
                            )

                    )
    )
    )

我尝试了下面的代码,但还没有成功,它没有给出所需的输出

$countries_data = $countries_data['data'];
$countries_att = array_column($countries_data, 'lat');
$countries_ids = array_column($countries_data, 'id');
$final = array_merge($countries_att, $countries_ids);
echo "<pre>";
print_r($final);

$res = array();
foreach($countries_att as $k => $v){
    $res[$k] = array_merge($countries_att[$k],$countries_ids[$k]);
}
echo "<pre>";
print_r($res);

我只想要以下格式的 id、代码、lat 和 lng

[0] => 
        [id] => 241
        [code] => AF
        [lat] => 33.93911
        [lng] => 67.709953
[1] => 
        [id] => 235
        [code] => AL
        [lat] => 41.153332
        [lng] => 20.168331

标签: phparraysjsonmultidimensional-arrayassociative-array

解决方案


考虑到以下 JSON 对象:

{
   "data":[
      {
         "id":"241",
         "type":"country",
         "attributes":{
            "code":"AF",
            "name":"Afghanistan",
            "lat":"33.93911",
            "lng":"67.709953"
         }
      },
      {
         "id":"235",
         "type":"country",
         "attributes":{
            "code":"AL",
            "name":"Albania",
            "lat":"41.153332",
            "lng":"20.168331"
         }
      },
      {
         "id":"236",
         "type":"country",
         "attributes":{
            "code":"DZ",
            "name":"Algeria",
            "lat":"28.033886",
            "lng":"1.659626"
         }
      }
   ]
}

循环遍历代码并仅获取您想要的元素的代码可能如下所示:

<?php

// Define our JSON locations object with our locations
$locations = '{"data":[{"id":"241","type":"country","attributes":{"code":"AF","name":"Afghanistan","lat":"33.93911","lng":"67.709953"}},{"id":"235","type":"country","attributes":{"code":"AL","name":"Albania","lat":"41.153332","lng":"20.168331"}},{"id":"236","type":"country","attributes":{"code":"DZ","name":"Algeria","lat":"28.033886","lng":"1.659626"}}]}';

// Decodes the JSON object and prints it out pretty-like
//print_r(json_decode($locations, true));

// Decode the JSON object to a PHP array
$locations = json_decode($locations, true);

// Loop through the newly created PHP array
$parsedLocations = [];

foreach ($locations['data'] AS $location)
{
  // Prints out the current location pretty-like
  //print_r($location);

  $id = $location['id'];
  $type = $location['type'];

  // This will be an array containing the items below
  $attributes = $location['attributes'];

  // $code = $attributes['code'];
  $code = $location['attributes']['code'];

  // $name = $attributes['name'];
  $name = $location['attributes']['name'];

  // $lat = $attributes['lat'];
  $lat = $location['attributes']['lat'];

  // $lng = $attributes['lng'];
  $lng = $location['attributes']['lng'];

  $parseLocations[] = array(
    'id' => $id,
    'type' => $type,
    'code' => $code,
    'name' => $name,
    'lat' => $lat,
    'lng' => $lng
  );
}

print_r($parseLocations);

上面的代码已经过测试并且可以工作。我已经向您展示了如何将值分配给已解析对象中的数据中的变量,以及如何将这些变量分配到新的嵌套数组中。你也可以这样做:

<?php

// Define our JSON locations object with our locations
$locations = '{"data":[{"id":"241","type":"country","attributes":{"code":"AF","name":"Afghanistan","lat":"33.93911","lng":"67.709953"}},{"id":"235","type":"country","attributes":{"code":"AL","name":"Albania","lat":"41.153332","lng":"20.168331"}},{"id":"236","type":"country","attributes":{"code":"DZ","name":"Algeria","lat":"28.033886","lng":"1.659626"}}]}';

// Decodes the JSON object and prints it out pretty-like
//print_r(json_decode($locations, true));

// Decode the JSON object to a PHP array
$locations = json_decode($locations, true);

// Loop through the newly created PHP array
$parsedLocations = [];

foreach ($locations['data'] AS $location)
{
  // Prints out the current location pretty-like
  //print_r($location);

  $parsedLocations[] = array(
    'id' => $location['id'],
    'type' => $location['type'],
    'code' => $location['attributes']['code'],
    'name' => $location['attributes']['name'],
    'lat' => $location['attributes']['lat'],
    'lng' => $location['attributes']['lng']
  );
}

print_r($parsedLocations);

如果我理解您的问题,这应该可以帮助您到达需要的地方。


推荐阅读