在一个键上组合两个数组?

时间:2009-09-08 08:41:41

标签: php arrays

如果我想通过相同的值将一个数组中的项与另一个数组相关联,例如。 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中已经实现了一个简洁的解决方案!非常感谢您的帮助。

3 个答案:

答案 0 :(得分:4)

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

<?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的项目(一个用于未定义的组 - 它将忽略这些项目)。

答案 1 :(得分:2)

如果用id而不是数字索引键入你的groups数组会更容易

$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
         );
       );
     );

答案 2 :(得分:0)

我找到了一个适合我的解决方案,并得到了解决方案的帮助。

$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;
            }
        }

感谢您的帮助!