Heim >Backend-Entwicklung >PHP-Tutorial >php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组
php 多维数组
举例时要自圆其说,不然会产生误解
原始数据中并没有:
'sv_id' => 5, 'mid' => 0
'sv_id' => 9, 'mid' => 3
的项,结果中是如何出来的?
举例时要自圆其说,不然会产生误解
原始数据中并没有:
'sv_id' => 5, 'mid' => 0
'sv_id' => 9, 'mid' => 3
的项,结果中是如何出来的?
额,是的,不过数据是可以随便改的啊,好好看看两张图的数组结构吧。。。
这根结构没什么关系吧?
从算法角度上看:是如何产生不在原始数据中的项
<?php $arr=array( array( 'sv_id'=>1, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>2, 'm_id'=>2, 'sv_question1'=>10 ), array( 'sv_id'=>3, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>4, 'm_id'=>3, 'sv_question1'=>20 ), array( 'sv_id'=>5, 'm_id'=>2, 'sv_question1'=>10 ) );//var_dump($arr);$ar=array();foreach($arr as $v){ if(empty($ar) || !in_array($v,$ar)){ $ar[$v['m_id']][$v['sv_id']]=$v; }else{ foreach($ar as $ka=>$va){ if($ka==$v['m_id']){ $ar[$ka][$v['sv_id']]=$v; } } }}echo "<pre class="brush:php;toolbar:false">";var_dump($ar);
不考虑你多出的项目(我无法臆造)
$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 ) ))
哇,#5版主,,,为啥我想不到你写的方法呢。。你的好简洁。
<?php $arr=array( array( 'sv_id'=>1, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>2, 'm_id'=>2, 'sv_question1'=>10 ), array( 'sv_id'=>3, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>4, 'm_id'=>3, 'sv_question1'=>20 ), array( 'sv_id'=>5, 'm_id'=>2, 'sv_question1'=>10 ) );//var_dump($arr);$ar=array();foreach($arr as $v){ if(empty($ar) || !in_array($v,$ar)){ $ar[$v['m_id']][$v['sv_id']]=$v; }else{ foreach($ar as $ka=>$va){ if($ka==$v['m_id']){ $ar[$ka][$v['sv_id']]=$v; } } }}echo "<pre class="brush:php;toolbar:false">";var_dump($ar);
不考虑你多出的项目(我无法臆造)
$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 ) ))
这根结构没什么关系吧?
从算法角度上看:是如何产生不在原始数据中的项
额,是,sorry 我写错,少了一个下划线 m_id 写成了mid
<?php $arr=array( array( 'sv_id'=>1, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>2, 'm_id'=>2, 'sv_question1'=>10 ), array( 'sv_id'=>3, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>4, 'm_id'=>3, 'sv_question1'=>20 ), array( 'sv_id'=>5, 'm_id'=>2, 'sv_question1'=>10 ) );//var_dump($arr);$ar=array();foreach($arr as $v){ if(empty($ar) || !in_array($v,$ar)){ $ar[$v['m_id']][$v['sv_id']]=$v; }else{ foreach($ar as $ka=>$va){ if($ka==$v['m_id']){ $ar[$ka][$v['sv_id']]=$v; } } }}echo "<pre class="brush:php;toolbar:false">";var_dump($ar);
<?php $arr=array( array( 'sv_id'=>1, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>2, 'm_id'=>2, 'sv_question1'=>10 ), array( 'sv_id'=>3, 'm_id'=>1, 'sv_question1'=>20 ), array( 'sv_id'=>4, 'm_id'=>3, 'sv_question1'=>20 ), array( 'sv_id'=>5, 'm_id'=>2, 'sv_question1'=>10 ) );//var_dump($arr);$ar=array();foreach($arr as $v){ if(empty($ar) || !in_array($v,$ar)){ $ar[$v['m_id']][$v['sv_id']]=$v; }else{ foreach($ar as $ka=>$va){ if($ka==$v['m_id']){ $ar[$ka][$v['sv_id']]=$v; } } }}echo "<pre class="brush:php;toolbar:false">";var_dump($ar);