Home >Backend Development >PHP Tutorial >How to count the number of array elements in PHP

How to count the number of array elements in PHP

WBOY
WBOYOriginal
2016-07-25 08:53:241181browse
  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_));

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn