我有 2 个数组$list和$list2:
$列表:
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、team_points、team_occurrences)
现在,我想将这两个数组合并到名为$list_all 的数组中
合并的问题是我尝试过的标准合并
array_merge($list,$list2);
只需将它们加在一起。
但是,我需要计算相同的团队,例如[team] => 14和[team] => 13都在两个数组中(在 $list 和 $list2 中),因此,我需要从 $list 中对team_points列值求和如果团队相同,则使用 $list2 中的和team_points列值。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 的一些更好的函数(从最高值到最低值)。
提前感谢您的任何建议。