php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组
比如这样的,我想将这个数组里面 m_id 相等数组元素再组合成一个新的数组:效果图如下:
[解决办法]
不考虑你多出的项目(我无法臆造)
$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
)
)
)