【问题标题】:From an array, group items based on the key value with PHP从数组中,使用 PHP 根据键值对项目进行分组
【发布时间】:2019-09-04 01:08:50
【问题描述】:

我在 PHP 中有这个多维数组:

Array
(
    [0] => Array
        (
            [continent] => Europa
            [country] => France
            [capital] => Paris
        )

    [1] => Array
        (
            [continent] => Europa
            [country] => Spain
            [capital] => Madrid
        )
    [2] => Array
        (
            [continent] => Asia
            [country] => Russia
            [capital] => Moscow
        )
)

如何根据大陆对国家/地区进行分组?

所以同一大陆的国家应该归为一组。

期望的输出应该是:

Array
(
    [Europa] => Array
        (
            [country] => France
            [capital] => Paris
        ),
        (
            [country] => Spain
            [capital] => Madrid
        )
    [Asia] => Array
        (
            [country] => Russia
            [capital] => Moscow
        )
)

请问用 PHP 怎么可能?

谢谢。

【问题讨论】:

  • 请展示您已经尝试过的内容...还是希望有人为您编写代码?
  • 这很容易。试试看,你会发现你自己做到了。这是关联数组最基本的用法。

标签: php arrays


【解决方案1】:
$groupByContinent = function(array $list) {
    return array_reduce($list, function($grouped, $item) {
        $grouped[$item['continent']][] = $item;
        return $grouped;
    }, []);
};

$groupedByContinent = $groupByContinent($data);

https://3v4l.org/s6X1c

或者:

$groupByProperty = function(array $list, string $property) {
    return array_reduce($list, function($grouped, $item) use(&$property) {
        $grouped[$item[$property]][] = $item;
        return $grouped;
    }, []);
};

$groupedByContinent = $groupByProperty($data, 'continent');

https://3v4l.org/Be3HL

【讨论】:

    【解决方案2】:

    你可以创建一个新的数组变量,循环遍历你的数据数组,检查是否已经创建了键,如果没有,创建它并将相关数据添加到该键。

    <?php
    // $data: the name of your array
    $grouped_data = array();
    
    for ($i = 0; $i < count($data); $i++) {
        $key = $data[$i]['continent'];
    
        if (!isset($grouped_data[$key])) {
            $grouped_data[$key] = array();
        }
    
        $grouped_data[$key][] = array(
            'country' => $data[$i]['country'],
            'capital' => $data[$i]['capital']
        );
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      你也可以试试:

      $data = array(
          array(  
              "continent" => "Europa",
              "country" => "France",
              "capital" => "Paris",
              ),
         array(
             "continent" => "Europa",
             "country" => "Spain",
             "capital" => "Madrid",
             ),
         array(
             "continent" => "Asia",
             "country" => "Russia",
             "capital" => "Moscow",
             )
      );
      
      
      $group_by = 'continent';
      $attributes = array( 'country', 'capital' );
      
      $output = array();
      foreach( $data as $country ) {
      
          if( isset( $country[$group_by] ) ) {
      
              $one = array();
              foreach( $attributes as $attribute ) {
                  if( isset( $country[$attribute] ) ) {
                      $one[$attribute] = $country[$attribute];
                  }
              }
              $output[ $country[$group_by] ][] = $one;
           }
      }
      
      
      print_r($output);
      

      Demo

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 2022-08-16
        • 1970-01-01
        • 1970-01-01
        • 2018-11-23
        • 1970-01-01
        相关资源
        最近更新 更多