2009-09-08 30 views
0

如果我想通过相同的值将一个阵列中的项目与另一个阵列相关联,例如。 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中!非常感谢您的帮助。

回答

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

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

感谢您的帮助!