php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组
展开全部
$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的总和”是什么意思
追问
$sum = array();
foreach($tmp as $itme){
foreach($itme as $i){
$sum[$i['mid_id']]+= $i['sv_question'];
}
}
最后输出:
Array
(
[1] => 40
[2] => 30
[3] => 20
)
试着做出来了,前面的多谢你啦!!!嘎嘎
展开全部
$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=
}
}
大概就这样吧!
foreach(){
if(){
$temp=
}
}
大概就这样吧!
追问
可是,if 里面的条件怎么判断呀
追答
$arr = array(
0=>array('sv_id'=>1,'m_id'=>1,'sv_question'=>20),
1=>array('sv_id'=>3,'m_id'=>2,'sv_question'=>20),
2=>array('sv_id'=>2,'m_id'=>1,'sv_question'=>20),
3=>array('sv_id'=>4,'m_id'=>3,'sv_question'=>20),
4=>array('sv_id'=>5,'m_id'=>2,'sv_question'=>20)
);
function countArr($arr){
$temp=array();
foreach($arr as $k=>$v){
$temp[$v['m_id']][$v['sv_id']]=$v;
}
return $temp;
}
print_r(countArr($arr));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询