Heim  >  Artikel  >  Backend-Entwicklung  >  PHP如何统计数组元素个数

PHP如何统计数组元素个数

WBOY
WBOYOriginal
2016-07-25 08:53:241117Durchsuche
  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_));
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn