2012-10-12 124 views
1

我有2個陣列$列表$列表2合併2個陣列在這種艱難的方式在PHP

$列表

Array 
(
[0] => Array 
    (
     [team] => 14 
     [team_points] => 3 
     [team_occurrences] => 2 
    ) 

[1] => Array 
    (
     [team] => 1 
     [team_points] => 3 
     [team_occurrences] => 2 
    ) 

[2] => Array 
    (
     [team] => 5 
     [team_points] => 1 
     [team_occurrences] => 1 
    ) 

[3] => Array 
    (
     [team] => 13 
     [team_points] => 1 
     [team_occurrences] => 1 
    ) 

[4] => Array 
    (
     [team] => 7 
     [team_points] => 0 
     [team_occurrences] => 1 
    ) 

[5] => Array 
    (
     [team] => 2 
     [team_points] => 0 
     [team_occurrences] => 3 
    ) 

) 

$列表2:

Array 
(
    [0] => Array 
     (
      [team] => 20 
      [team_points] => 7 
      [team_occurrences] => 3 
     ) 

    [1] => Array 
     (
      [team] => 10 
      [team_points] => 3 
      [team_occurrences] => 1 
     ) 

    [2] => Array 
     (
      [team] => 14 
      [team_points] => 3 
      [team_occurrences] => 1 
     ) 

    [3] => Array 
     (
      [team] => 13 
      [team_points] => 3 
      [team_occurrences] => 1 
     ) 

    [4] => Array 
     (
      [team] => 19 
      [team_points] => 3 
      [team_occurrences] => 1 
     ) 

    [5] => Array 
     (
      [team] => 17 
      [team_points] => 1 
      [team_occurrences] => 1 
     ) 

    [6] => Array 
     (
      [team] => 11 
      [team_points] => 0 
      [team_occurrences] => 1 
     ) 

    [7] => Array 
     (
      [team] => 15 
      [team_points] => 0 
      [team_occurrences] => 1 
     ) 

) 

正如你所看到的列是在兩個數組(團隊,team_points,team_occurrences)

現在一樣,我想這兩個數組合併成陣列稱爲$ list_all

與合併的問題是,標準的合併我已經試過

array_merge($list,$list2); 

只是將它們加在一起。

但是,我需要的是統計相同的隊伍,例如[團隊] => 14[組] => 13在兩種陣列(在$列表和列表2 $),因此,因此,我需要總結從與$列表team_points列值和team_points如果團隊相同,則從$ list2列值。 team_occurrences列相同。

所以例如

新的陣列將是這個樣子:

Array 
(
[0] => Array // from $list 
    (
     [team] => 14 
     [team_points] => 3 
     [team_occurrences] => 2 
    ) 

[1] => Array // from $list2 
    (
     [team] => 14 
     [team_points] => 3 
     [team_occurrences] => 1 
    ) 

[3] => Array // from $list 
    (
     [team] => 13 
     [team_points] => 1 
     [team_occurrences] => 1 
    ) 

[4] => Array // from $list2 
    (
     [team] => 13 
     [team_points] => 3 
     [team_occurrences] => 1 
    ) 

但我需要它看起來就像這樣:

Array 
(
[0] => Array 
    (
     [team] => 14 
     [team_points] => 6 
     [team_occurrences] => 3 
    ) 

[1] => Array 
    (
     [team] => 13 
     [team_points] => 4 
     [team_occurrences] => 2 
    ) 

合併之後,我想利用usort結果數組排序( )或者可以通過team_points DESC(從最高值到最低值)獲得更好的功能。

在此先感謝您的任何建議。

+0

你能不能改you'r sql語句? –

+0

不,我需要處理數組有很多原因:( – Derfder

+2

呃......除了for循環之外沒有任何東西可以跨越我的想法... –

回答

2

迭代第一個列表,創建一個新的數組,其中的鍵是團隊號碼。然後遍歷第二個數組,如果團隊已經存在,則添加或以其他方式追加。

$combined = array(); 
// First loop creates array keys in $combined array 
// based on team numbers 
foreach($list as $initial) { 
    $combined[$initial['team']] = $initial; 
} 

// Second loop looks at $list2 and either adds to values 
// if the team key already exists, or just adds the 
// whole subarray on if it doesn't. 
foreach($list2 as $secondary) { 
    // If it was in the first, append to it. 
    if (isset($combined[$secondary['team']])) { 
    $combined[$secondary['team']]['team_points'] += $secondary['team_points']; 
    $combined[$secondary['team']]['team_occurrences'] += $secondary['team_occurrences']; 
    } 
    // Otherwise, just add it to the array 
    else { 
    $combined[$secondary['team']] = $secondary; 
    } 
} 

// Then sort the combined array with usort() into descending order by points 
// Anonymous callback function is PHP 5.3+. Needs to be a string value of a named function 
// for PHP < 5.3 
usort($combined, function($a, $b) { 
    if ($a['team_points'] === $b['team_points']) { 
    return 0; 
    } 
    else { 
    return $a['team_points'] < $b['team_points'] ? 1 : -1; 
    } 
}); 

注意:以上產生按團隊編號鍵入的輸出數組。如果你真的只是想上升的數字鍵,通話array_values()它:

// Strip off the team number keys and just use ascending numbers 
$combined = array_values($combined); 

編輯修正上述幾個失蹤] ......

+0

如果團隊存在B和不在初始?? – Baba

+0

@Baba然後它只是被添加到'$ combined'。我誤解了需求嗎? –

+0

是的,然後只是添加。那好吧,我會嘗試你的代碼,並讓你知道 – Derfder

1

最簡單的就是選對團隊人數都陣列(使用usort)。同時瀏覽兩個數組,並選擇最低的團隊編號。如果list1最低,則將該團隊添加到結果並增加list1索引。如果list2最低,則將該團隊添加到結果並增加list2索引。如果兩隊相同,則通過添加點和出現次數來組合它們。重複,直到你在一個列表的末尾,並添加其他列表中的剩餘元素。

1

你可以做到這一點通過以下方式:

foreach($list2 as $ky => $val) { 
    $list2['t'.$val['team']] = $val; 
} 

$tot = count($list1); 
$list3 = array(); 
for ($i=0;$i<$tot;$i++) { 
    $team = $list1[$i]['Team']; 
    $list3[]['Team'] = $team; 
    $points = isset($list2['t'+$team]) ? $list2['t'+$team]['team_points'] : 0; 
    $list3[]['team_points'] = $list1[i]['team_points'] + $points; 
    $ocu = isset($list2['t'+$team]) ? $list2['t'+$team]['team_ocurrences'] : 0; 
    $list3[]['team_ocurrences'] = $list1[$i]['team_ocurrences'] + $ocu; 
} 

function cmp($a, $b) { 
    if ($a['team_points'] == $b['team_points']) { 
     return 0; 
    } 
    return ($a['team_points'] > $b['team_points']) ? -1 : 1; 
} 

usort($list3, "cmp"); 
+0

我會嘗試你的解決方案,並讓你知道它是否有效。 – Derfder

+0

我給出了錯誤警告:usort()期望參數1是數組,空給出 – Derfder

+0

@Derfder即使其他答案適用於您並接受它,我仍然想知道我的上次更新是否也最終適用於您。 。:-) – Nelson