我有点混淆如何基于公共值重新组合数组。这是下面的数组:
Array
(
[0] => Array
(
[team] => 1
[id] => 5
[user] => teamleader1
[Designation] => Team Leader
)
[1] => Array
(
[team] => 1
[id] => 6
[user] => consultant1
[Designation] => Consultant
)
[2] => Array
(
[team] => 1
[id] => 7
[user] => consultant2
[Designation] => Consultant
)
[3] => Array
(
[team] => 2
[id] => 8
[user] => consultant3
[Designation] => Consultant
)
[4] => Array
(
[team] => 2
[id] => 9
[user] => teamleader2
[Designation] => Team Leader
)
)
我希望按照下面的团队价值对其进行分组:
Array ( [1] => Array ( [0] => Array( [team] => 1 [id] => 5 [user] =>teamleader1 [Designation] => Team Leader ) [1] => Array( [team] => 1 [id] => 6 [user] =>consultant1 [Designation] => Consultant ) [2] => Array( [team] => 1 [id] => 7 [user] =>consultant2 [Designation] => Consultant ) ) [2] => Array ( [0] => Array( [team] => 1 [id] => 8 [user] =>consultant3 [Designation] => Consultant ) [1] => Array( [team] => 1 [id] => 9 [user] =>teamleader2 [Designation] => Team Leader ) ) )
两个主要阵列组是团队本身。任何想法/帮助将不胜感激。提前谢谢!
此致
答案 0 :(得分:2)
<?php
$grouped = array();
foreach ($yourData as $item) {
// copy item to grouped
$grouped[$item['team']][] = $item;
}
var_dump($grouped);