计算多维数组中的值?

时间:2013-02-06 02:42:50

标签: php arrays

我有一个大型数组,我基本上需要计算唯一数量: example array

最终结果我正在寻找像

这样的东西
$result = array(
    'A3D5' => array('P2' => 1, 'P3' => 1, 'P4' => 1, 'total' => 3),
    'AE5S' => array('P4' => 1, 'total' => 1)
);

我尝试过预习但似乎无法弄清楚如何将它们分类为另一个键,例如$zone = "P{$array['zone']}"; $result[$zone][$prestige]++或似乎有点不起作用或只是错误。

1 个答案:

答案 0 :(得分: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>";