$arr = array(
array('sv_id'=>1,'mid_id'=>1,'sv_question'=>20),
array('sv_id'=>2,'mid_id'=>2,'sv_question'=>10),
array('sv_id'=>3,'mid_id'=>1,'sv_question'=>20),
array('sv_id'=>4,'mid_id'=>3,'sv_question'=>20),
array('sv_id'=>5,'mid_id'=>2,'sv_question'=>20),
);
foreach($arr as $k => $val){
if(!isset($tmp[$val['mid_id']])){
$tmp[$val['mid_id']][$val['sv_id']] = array( 'sv_id'=>$val['sv_id'], 'mid_id'=>$val['mid_id'], 'sv_question'=>$val['sv_question'], );
}else{
$tmp[$val['mid_id']][$val['sv_id']] = array( 'sv_id'=>$val['sv_id'], 'mid_id'=>$val['mid_id'], 'sv_question'=>$val['sv_question'], );
}
}
print_r($tmp);
#效果是一样,但是不理解“计算sv_qustion的总和”是什么意思
$arr = array(
0=>array('sv_id'=>1,'mid_id'=>1,'sv_question'=>20),
1=>array('sv_id'=>3,'mid_id'=>2,'sv_question'=>20),
2=>array('sv_id'=>2,'mid_id'=>1,'sv_question'=>20)
);
function countArr($arr)
{
$arr2 = array();
foreach($arr as $k=>$v)
{
foreach($v as $k2=>$v2)
{
if(!isset($arr2[$k2][$v2]))
{
$arr2[$k2][$v2] = 1;
}
else
{
++$arr2[$k2][$v2];
}
}
}
return $arr2;
}
print_r(countArr($arr));
输出:
Array (
[sv_id] => Array ( [1] => 1 [3] => 1 [2] => 1 )
[mid_id] => Array ( [1] => 2 [2] => 1 )
[sv_question] => Array ( [20] => 3 ) )
$temp=array();
foreach(){
if(){
$temp=
}
}
大概就这样吧!