当前位置:Gxlcms > PHP教程 > PHP如何统计数组元素个数

PHP如何统计数组元素个数

时间:2021-07-01 10:21:17 帮助过:27人阅读

  1. $arr = array(

  2. '1011,1003,1008,1001,1000,1004,1012',
  3. '1009',
  4. '1011,1003,1111'
  5. );
  6. $result = array();
  7. foreach ($arr as $str) {
  8. $str_arr = explode(',', $str);
  9. foreach ($str_arr as $v) {
  10. $result[$v] = isset($result[$v]) ? $result[$v] : 0;
  11. $result[$v] = $result[$v] + 1;
  12. }
  13. }
  14. print_r($result);
  15. echo '
    ';
  16. $set=array();
  17. array_walk($arr,function($c) use (&$set ) {
  18. $elements=explode(',',$c);
  19. array_walk($elements,function($d) use(&$set){
  20. isset($set[$d])? $set[$d]++:$set[$d]=1;
  21. });
  22. });

  23. print_r($set);

  24. echo '
    ';
  25. $arr[0] = array(1011,1003,1008,1001,1000,1004,1012);
  26. $arr[1] = 1009;
  27. $arr[2] =array(1011,1003,1111);
  28. function m2s($arr){
  29. static $new_arr = array();
  30. foreach($arr as $k=>$v){
  31. if(is_array($v)){
  32. m2s($v);

  33. }else{

  34. $new_arr[]=$v;
  35. }
  36. }
  37. return $new_arr;
  38. }
  39. $arr_ = m2s($arr);
  40. print_r(array_count_values ($arr_));

人气教程排行