PHP将数组分成基于相等值的组

前端之家收集整理的这篇文章主要介绍了PHP将数组分成基于相等值的组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Array包含2个值的数组,第一个是作者的编号,第二个是他的隶属关系.
Array ( 
    [0] => Array ( 
            [0] => 2 
            [1] => Department of General Chemistry
        ) 
    [1] => Array ( 
            [0] => 3 
            [1] => Institute of Silicate Materials
        ) 
    [2] => Array ( 
            [0] => 4 
            [1] => Department of General Chemistry
        ) 
    [3] => Array ( 
            [0] => 5 
            [1] => Department of General Chemistry
        ) 
    [4] => Array ( 
            [0] => 6 
            [1] => Institute of Silicate Materials
        ) 
)

如果隶属关系相同,我如何分组作者?我需要输出如下:

3,6 Institute of Silicate Materials
2,4,5 Department of General Chemistry
foreach ($array as $key => $value) {
 $return[$value[1]][] = $value[0];
}

foreach ($return as $key => $value) {
  echo implode(',',$value)." ".$key;
}

猜你在找的PHP相关文章