我有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(從最高值到最低值)獲得更好的功能。
在此先感謝您的任何建議。
你能不能改you'r sql語句? –
不,我需要處理數組有很多原因:( – Derfder
呃......除了for循環之外沒有任何東西可以跨越我的想法... –