本文介绍了PHP根据相等的值将数组分成组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个值的数组的数组,第一个是作者的编号,第二个是他的隶属关系.

I have an Array containing arrays with 2 values, the first one is the Number of the Author the second is his Affiliation.

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
        )
)

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

How can I group the Authors if the Affiliation is the same? I need the output to be something like:

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根据相等的值将数组分成组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:33