Array (
[0] => 1011,1003,1008,1001,1000,1004,1012
[1] => 1009
[2] => 1011,1003,1111
)
我的数组是这样的。
我现在想统计出
Array
{
[1011]=>1 //意思是说有几个
[1001]=>2
}
要怎么样统计呢
$arr = array(
'1011,1003,1008,1001,1000,1004,1012',
'1009',
'1011,1003,1111'
);
$result = array();
foreach ($arr as $str) {
$str_arr = explode(',', $str);
foreach ($str_arr as $v) {
$result[$v] = isset($result[$v]) ? $result[$v] : 0;
$result[$v] = $result[$v] + 1;
}
}
print_r($result);
echo '<br />';
$set=array();
array_walk($arr,function($c) use (&$set ) {
$elements=explode(',',$c);
array_walk($elements,function($d) use(&$set){
isset($set[$d])? $set[$d]++:$set[$d]=1;
});
});
print_r($set);
echo '<br />';
$arr[0] = array(1011,1003,1008,1001,1000,1004,1012);
$arr[1] = 1009;
$arr[2] =array(1011,1003,1111);
function m2s($arr){
static $new_arr = array();
foreach($arr as $k=>$v){
if(is_array($v)){
m2s($v);
}else{
$new_arr[]=$v;
}
}
return $new_arr;
}
$arr_ = m2s($arr);
print_r(array_count_values ($arr_));
[转]PHP统计数组元素个数
最新推荐文章于 2024-03-03 21:16:32 发布