解析数组并计算

时间:2011-08-02 21:02:56

标签: php cakephp-1.3

我有一组庞大的数组,如下图所示。我想算一下汽车出现在同一厂家下的次数。我该怎么做

[0] => Array
        (
            [Make] => Array
                (
                    [ManufacturersName] => Nissan
                    [type] => 4Dr
                    [manufacturersID] => 1
                )

            [Car] => Array
                (
                    [Model] => Mexima

                )


        )
[1] => Array
        (
            [Make] => Array
                (
                    [ManufacturersName] => Nissan
                    [type] => 4Dr
                    [manufacturersID] => 1
                )

            [Car] => Array
                (
                    [Model] => Mexima

                )


        )

[2] => Array
        (
            [Make] => Array
                (
                    [ManufacturersName] => Toyota
                    [type] => 4Dr
                    [manufacturersID] => 2
                )

            [Car] => Array
                (
                    [Model] => Corolla

                )


        )

在上面的示例中,Maxima在Manufacturerid 1中出现了两次。

由于

1 个答案:

答案 0 :(得分:1)

你可以在这里使用数组键的属性

$count_bucket = array();

foreach ($arr as $a) {
    $manufacturer = $a['Make']['Manufacturer_id'];
    $car = $a['Car']['Model'];
    $count_bucket[$manufacturer][$car]++;
}

的var_dump($ count_bucket);