Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count number of occurrences of a key in a multidimensional array

I want to calculate the average from a key contained in the array.

Array
(
    [123456] => Array
        (
            [Log] => 2793
            [Approve] => 1724
            [Accept] => 13863
        )

    [123457] => Array
        (
            [Log] => 2606
            [Classify] => 730
            [FirstLineSupport] => 83339
            [Escalate] => 14689501
            [Fulfill] => 14772840
        )

    [123458] => Array
        (
            [Log] => 2700
            [Approve] => 16152972
            [Fulfill] => 73006092
            [Accept] => 729914
            [Review] => 27033
        )
)

In this case I need to calculate the average of key "Fulfill".
So I need to calculate:

(14772840 + 73006092) / 2 = 43889466

I know I can sum the values using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$sum = array_sum(array_column($arr, 'Fulfill'));

But how can I divide by the number of occurrences of the key?

PHP online: https://onlinephp.io/c/35153

>Solution :

Just one line more:

$args = array_column($arr, 'Fulfill');
$sum = array_sum($args)/count($args);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading