dousuowu4610 2013-07-05 18:49
浏览 36
已采纳

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

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
  • 写回答

4条回答 默认 最新

  • dtsjq28482 2013-07-05 18:55
    关注
    foreach ($array as $key => $value) {
     $return[$value[1]][] = $value[0];
    }
    
    foreach ($return as $key => $value) {
      echo implode(',', $value)." ".$key;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?