Home > Mobile >  Combine array with same value and add other
Combine array with same value and add other

Time:05-08

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:

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?

CodePudding user response:

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
  • Related