我想在多维数组的最深/最后一级计算相同的值:
Array
(
[svote001] => Array
(
[0] => 006
[1] => 006
[2] => 007
)
[svote002] => Array
(
[0] => 000
[1] => 000
[2] => 000
)
[svote003] => Array
(
[0] => 002
[1] => 003
[2] => 001
)
)
转换为
Array
(
[svote001] => Array
(
[006] => 2
[007] => 1
)
[svote002] => Array
(
[000] => 3
)
[svote003] => Array
(
[001] => 1
[002] => 1
[003] => 1
)
)
计数值还应该从高到低的数字排序。
答案 0 :(得分:6)
foreach($array as $k => $v) {
$result[$k] = array_count_values($v);
arsort($result[$k]);
}
答案 1 :(得分:2)
这应该符合您的需求:
<?php
$a = array(
'svote001' => array("006", "006", "007"),
'svote002' => array("000", "000", "000"),
'svote003' => array("003", "001", "001"),
);
$resultArray = array();
foreach ($a as $arrayName => $arrayData){
$resultArray[$arrayName] = array();
foreach ($arrayData as $value){
if (empty($resultArray[$arrayName][$value])){
$resultArray[$arrayName][$value] = 1;
}else{
$resultArray[$arrayName][$value]++;
}
}
arsort($resultArray[$arrayName]);
}
var_dump($resultArray);
编辑:AbraCadaver解决方案要好得多,请使用此解决方案。无论如何,我会让答案留在这里。