【问题标题】:Combine two arrays on a key?在一个键上组合两个数组?
【发布时间】:2009-09-08 08:41:41
【问题描述】:

如果我想通过相同的值将一个数组中的项目与另一个数组相关联,例如。 items.group_id -> groups.group_id,是否有一个数组函数可以巧妙地做到这一点? =)

我有两个数组:

$items = array(
              [0] => array(
                          'group_id' => 456,
                          'item_id' => 123,
                          // Rest of details
                     );
              [1] => array(
                          'group_id' => 457,
                          'item_id' => 124,
                          // Rest of details
                     );
              [2] => array(
                          'group_id' => 457,
                          'item_id' => 125,
                          // Rest of details
                     );
              [3] => array(
                          'group_id' => 456,
                          'item_id' => 126,
                          // Rest of details
                     );
          );

$groups = array(
                [0] => array(
                          'group_id' => 456,
                          'group_name' => 'General'
                     );
                [1] => array(
                          'group_id' => 457,
                          'group_name' => 'Ungeneral'
                     );
          );

而我想要的结果是:

$groups = array(
                [0] => array(
                          'group_id' => 456,
                          'group_name' => 'General'
                          [0] => array(
                                     'item_id' => 123,
                                     // Rest of details
                                 );
                          [1] => array(
                                     'item_id' => 126,
                                     // Rest of details
                                 );
                     );
                [1] => array(
                          'group_id' => 457,
                          'group_name' => 'Ungeneral'
                          [0] => array(
                                     'item_id' => 124,
                                     // Rest of details
                                 );
                          [1] => array(
                                     'item_id' => 125,
                                     // Rest of details
                                 );
                     );
          );

这可能并不太复杂,但我希望已经在 PHP 中实现了一个简洁的解决方案!非常感谢您的帮助。

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    我认为没有内置函数可以执行此操作,但这里有一些应该处理它的基本代码:

    <?php
        // First, group the items by their group_id
        $items_by_group = array();
        foreach($items as $item) {
            $items_by_group[$item['group_id']][] = $item;
        }
    
        // Second, go through the groups and if they have any associated items,
        // add them to that group's array.
        foreach($groups as $key => $value) {
            if(isset($items_by_group[$value['group_id']])) {
                foreach($items_by_group[$value['group_id']] as $ikey => $ivalue) {
                    unset($ivalue['group_id']);
                    $groups[$key][$ikey] = $ivalue;
                }
            }
        }
    ?>
    

    请注意,上面的代码可以安全地处理您拥有无效组 ID 的项目的情况(一个用于未定义的组 - 它会忽略这些项目)。

    【讨论】:

      【解决方案2】:

      如果您通过 id 而不是数字索引来键入您的组数组会更容易

      $newArray = array();
      foreach($groups as $group) {
        // add the group, and an items array we can append to later.
        $newArray[$group['group_id']] = $group;
        $newArray[$group['group_id']]['items'] = array();  
      }
      foreach ($items as $item) {
        $newArray[$item['group_id']]['items'][] = $item;
      }
      

      应该产生这样的数组:

      $newArray = array(
         [456] => array(
           'group_id' => 456,
           'group_name' => 'General'
           'items' => array(
             [0] => array(
               'item_id' => 123,
               // Rest of details
             );
             [1] => array(
               'item_id' => 126,
               // Rest of details
               );
             );
           );
      

      【讨论】:

        【解决方案3】:

        在您的解决方案小伙子的帮助下,我找到了适合我的解决方案。

        $links_by_group = array();
                foreach($links as $link) {
                    $linked_groups = $this->return_linked_groups($link['link_id']);
                    foreach($linked_groups as $group){
                        $links_by_group[$group][$link['link_id']] = $link;
                    }
                }
        

        感谢您的帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-12
          • 2013-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-16
          • 1970-01-01
          相关资源
          最近更新 更多