2013-02-06 90 views
0

我有一個大陣,我基本上需要計算不重複的號碼: example array在多維數組中計數值?

我在尋找類似

$result = array(
    'A3D5' => array('P2' => 1, 'P3' => 1, 'P4' => 1, 'total' => 3), 
    'AE5S' => array('P4' => 1, 'total' => 1) 
); 

最終的結果我已經試過foreaching通過,但不能似乎弄清楚如何將它們分類到另一個鍵,例如$zone = "P{$array['zone']}"; $result[$zone][$prestige]++,或者似乎有點不起作用或者僅僅是錯誤。

+0

您的示例數組中沒有'A35S'。 – Antony

+0

'AE5S'的'聲望'值爲2,而不是'P4'。 – Antony

+0

已修復,我正在計算某個值的發生次數,而不是數值。 – Dylan

回答

0
$array = array(
    "denizen" => array 
     (
      "prestige" => 2, 
      "zone" => "A3D5", 
      "scope_frag" => 765 
     ), 

    "생각" => array 
     (
      "prestige" => 4, 
      "zone" => "A3D5", 
      "scope_frag" => 135 
     ), 

    "Ryans" => array 
     (
      "prestige" => 3, 
      "zone" => "A3D5", 
      "scope_frag" => 78 
     ), 

    "지적인" => array 
     (
      "prestige" => 2, 
      "zone" => "AE5S", 
      "scope_frag" => 481 
     ) 
); 
foreach ($array as $group) { 
    $zones[$group["zone"]][] = $group["prestige"]; 
} 
foreach ($zones as $name => $zone) { 
    $total = count($zone); 
    foreach ($zone as $prestige) { 
     $result[$name]["P".$prestige] += 1; 
    } 
    ksort($result[$name]); 
    $result[$name]["total"] = $total; 
} 
echo "<pre>"; 
print_r($result); 
echo "</pre>";