开发者

parse through an array and count

I have a huge set of arrays like the one below. I want to count how many times a car is shown up under same manufacturersid. How would I do that

[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
                (
  开发者_StackOverflow中文版                  [Model] => Mexima

                )


        )

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

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

                )


        )

In above sample, Maxima showed up twice in Manufacturerid 1.

Thanks


you can use the properties of array keys to your advantage here

$count_bucket = array();

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

var_dump($count_bucket);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜