Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我有来自API的json数据,希望使用PHP从响应中提取数据_Php_Arrays_Json_Multidimensional Array_Associative Array - Fatal编程技术网

我有来自API的json数据,希望使用PHP从响应中提取数据

我有来自API的json数据,希望使用PHP从响应中提取数据,php,arrays,json,multidimensional-array,associative-array,Php,Arrays,Json,Multidimensional Array,Associative Array,我有来自API的json数据,格式如下。我只想从中提取选定的数据,比如id、代码、lat和lang。有人能帮忙吗?我使用的是核心PHP $countries_data = json_decode($countries, true); print_r($countries_data); Array ( [data] => Array ( [0] => Array (

我有来自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);

考虑到以下JSON对象:

<?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);
循环代码并仅获取所需元素的代码可以如下所示:


您有没有尝试过任何可以展示给我们的东西?更新了,请检查您有没有尝试调试该代码?工作非常完美,非常感谢您的帮助
{
   "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);