有如下的php数组:
现在想统计出:
php统计数组元素个数的代码:
<?php
$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_));