2013-08-02 110 views
0

是否有一種簡單的方法來計算多維數組中的值的出現次數?多維數組中返回出現

我有以下數組:

array(
    (int) 0 => array(
     'HrEmployee' => array(
      'id' => '7', 
      'surname' => 'Basson', 
      'name' => 'Andries' 
     ), 
     'Setup' => array(
      'color' => '004356', 
      'size' => 12 
    ), 
    (int) 1 => array(
     'HrEmployee' => array(
      'id' => '25', 
      'surname' => 'Coetzee', 
      'name' => 'Zandereen', 
     ), 
     'Setup' => array(
      'color' => 'CFCFCF', 
      'size' => 8 
    ), 
    (int) 2 => array(
     'HrEmployee' => array(
      'id' => '30', 
      'surname' => 'De Kock', 
      'name' => 'Theo', 
     ), 
     'Setup' => array(
      'color' => 'FFFFFF', 
      'size' => 12 
    ), 
    (int) 3 => array(
     'HrEmployee' => array(
      'id' => '31', 
      'surname' => 'De Vaal', 
      'name' => 'Chantell', 
     ), 
     'Setup' => array(
      'color' => '00FF00', 
      'size' => 10 
    ), 
) 

我想檢查的時候一定規模出現的量,所以它應該返回:

array(
    (int) 8 => (int) 1, 
    (int) 10 => (int) 1, 
    (int) 12 => (int) 2 
) 

如何可以完成這個?

+0

在我的經驗,你真的需要去深。在陣列上循環檢查'大小'並數它.. –

回答

1

應該像這樣工作:

$sizes = array(); 
foreach($arr as $v) { 
    $s = $v['Setup']['size']; 
    if(!isset($sizes[$s])){ 
     $sizes[$s] = 0; 
    } 
    $sizes[$s]++; 
} 

print_r($sizes); 
+0

太棒了。簡短而甜美。我過度想到了我的腦海。謝謝! – Albert