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

Combine array with same value and add other

I’m trying to find a way to combine array with the same value, and add other values of associative array.

I have an array of something like this:

Array
(
    [0] => Array
        (
            [count] => 3
            [date] => 2022-05-05
            [audio_click] => 2
        )

    [1] => Array
        (
            [count] => 1
            [date] => 2022-05-06
            [audio_click] => 0
        )

    [2] => Array
        (
            [count] => 4
            [date] => 2022-05-05
            [audio_click] => 5
        )

    [3] => Array
        (
            [count] => 18
            [date] => 2022-05-06
            [audio_click] => 14
        )

    [4] => Array
        (
            [count] => 1
            [date] => 2022-05-07
            [audio_click] => 0
        )

)

The output should be like this:

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

Array
(
    [0] => Array
        (
            [count] => 7
            [date] => 2022-05-05
            [audio_click] => 7
        )

    [1] => Array
        (
            [count] => 19
            [date] => 2022-05-06
            [audio_click] => 14
        )

    [2] => Array
        (
            [count] => 1
            [date] => 2022-05-07
            [audio_click] => 0
        )

)

So far I implemented the following code:

$data = $map_data_array;
//$data is the array in which you have data originally
foreach($data as $arrayK => $arrayV){
        $count_st = $data[$arrayV['date']]['count'] + $arrayV['count'];
        $data[$arrayV['date']]['count'] = $count_st;

        $audio_click = $data[$arrayV['date']]['audio_click'] + $arrayV['audio_click'];
        $data[$arrayV['date']]['audio_click'] = $audio_click;
}

But it’s giving me the following output:

Array
(
    [0] => Array
        (
            [count] => 3
            [date] => 2022-05-05
            [audio_click] => 2
        )

    [1] => Array
        (
            [count] => 1
            [date] => 2022-05-06
            [audio_click] => 0
        )

    [2] => Array
        (
            [count] => 4
            [date] => 2022-05-05
            [audio_click] => 5
        )

    [3] => Array
        (
            [count] => 18
            [date] => 2022-05-06
            [audio_click] => 14
        )

    [4] => Array
        (
            [count] => 1
            [date] => 2022-05-07
            [audio_click] => 0
        )

    [2022-05-05] => Array
        (
            [count] => 7
            [audio_click] => 7
        )

    [2022-05-06] => Array
        (
            [count] => 19
            [audio_click] => 14
        )

    [2022-05-07] => Array
        (
            [count] => 1
            [audio_click] => 0
        )

)

Not sure, what I’m doing wrong here?

>Solution :

Your approach is already performing the aggregation correctly, but you’ll have to do this in a new array, not the original array. And at the end of the process, you maybe want to turn the associative result array back to an indexed array (so no longer with date values as keys, but just 0, 1, 2…):

foreach($data as $key => $elem) {
    if (!isset($result[$elem["date"]])) {
        $result[$elem["date"]] = $elem;
    } else {
        $result[$elem["date"]]["count"] += $elem['count'];
        $result[$elem["date"]]["audio_click"] += $elem['audio_click'];
    }
}
$result = array_values($result); // optional
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