如何计算通用数组中的每个唯一元素


How to get a count each unique element in the general array

您好。

代码:

array(4) { 
    [0]=> array(1) { 
          [0]=> array(3) { 
               [0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950) 
          }
          [1]=> array(3) { 
               [0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)  
          } 
          [2]=> array(3) { 
               [0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)  
          }
          [3]=> array(3) { 
               [0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)  
          }
}

我得到数组唯一元素:

$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
    if(!in_array($elms[0], $arr_uniq)) {
        $arr_uniq[] = $elms[0];
    }
}

请告诉我如何计算通用数组中的每个唯一元素

结果应该是下一个:

art_7880-3

art_7883-1

假设$all_arrayvar_dump狙击手中主阵列的子阵列,那么一般的想法就是

$result = array();
foreach ($all_array as $elms)    
    $result[$elms[0]]++;    

array_count_values()

http://php.net/array_count_values

您应该能够轻松地应用此功能。