统计数组中相同的某一元素的其他 指定元素总和

时间:2022-03-14 19:00:28


http://bbs.csdn.net/topics/390590251



$ar  array (
   array 'sv_id'  => 1,  'mid'  => 1,  'sv_question1'  => 20 ),
   array 'sv_id'  => 2,  'mid'  => 2,  'sv_question1'  => 10 ),
   array 'sv_id'  => 3,  'mid'  => 1,  'sv_question1'  => 20 ),
   array 'sv_id'  => 4,  'mid'  => 3,  'sv_question1'  => 20 ),
   array 'sv_id'  => 5,  'mid'  => 2,  'sv_question1'  => 20 ),
);
$res  array ();
 
foreach ( $ar  as  $item ) {
   $res [ $item [ 'mid' ]][ $item [ 'sv_id' ]] =  $item ;
}
print_r( $res );



Array
(
     [1] => Array
         (
             [1] => Array
                 (
                     [sv_id] => 1
                     [mid] => 1
                     [sv_question1] => 20
                 )
 
             [3] => Array
                 (
                     [sv_id] => 3
                     [mid] => 1
                     [sv_question1] => 20
                 )
 
         )
 
     [2] => Array
         (
             [2] => Array
                 (
                     [sv_id] => 2
                     [mid] => 2
                     [sv_question1] => 10
                 )
 
             [5] => Array
                 (
                     [sv_id] => 5
                     [mid] => 2
                     [sv_question1] => 20
                 )
 
         )
 
     [3] => Array
         (
             [4] => Array
                 (
                     [sv_id] => 4
                     [mid] => 3
                     [sv_question1] => 20
                 )
 
         )
 
)