2009-09-08 35 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; 
      } 
     } 

感謝您的幫助!