Home  >  Article  >  Backend Development  >  php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组

php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组

WBOY
WBOYOriginal
2016-06-23 14:22:271250browse

php  多维数组 


比如这样的,我想将这个数组里面 m_id 相等数组元素再组合成一个新的数组:效果图如下:



回复讨论(解决方案)

举例时要自圆其说,不然会产生误解

原始数据中并没有:
'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);

你复制运行一下看是不是你要的效果。
效果图:



嗯,这个办法可以,谢啦, 不过下面的大神更恐怖,太简单了!!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn