0

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
                (
                    [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

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

You may want to add a few checks to see whether the manufacturer array and the car key already exist or not, and add them if needed. Otherwise PHP will complain (softly, but complain nonetheless)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.