简体   繁体   中英

PHP array reorganize with unique count

I have following array which has ID as index, and some count as value. Ultimate goal is to get total of unique IDs in another array.

Array
(
    [0] => Array
        (
            [0] => Team Object
                (
                    [id] => 1
                    [countStat] => 25
                )
        )
    [1] => Array
        (
            [0] => Team Object
                (
                    [id] => 2
                    [countStat] => 24
                )
        )
    [2] => Array
        (
            [0] => Team Object
                (
                    [id] => 1
                    [countStat] => 23
                )
        )
    [3] => Array
        (
            [0] => Team Object
                (
                    [id] => 3
                    [countStat] => 23
                )
        )
    [4] => Array
        (
            [0] => Team Object
                (
                    [id] => 5
                    [countStat] => 21
                )
        )
    [5] => Array
        (
            [0] => Team Object
                (
                    [id] => 3                    
                    [countStat] => 21
                )
        )
    [6] => Array
        (
            [0] => Team Object
                (
                    [id] => 5
                    [countStat] => 20
                )
        )
    [7] => Array
        (
            [0] => Team Object
                (
                    [id] => 2
                    [countStat] => 20
                )
        )
)

I want result like below.

Array
(
    [0] => Array
        (
            [0] => Team Object
                (
                    [id] => 1
                    [countStat] => 48
                )
        )
    [1] => Array
        (
            [0] => Team Object
                (
                    [id] => 2
                    [countStat] => 44
                )
        )

    [3] => Array
        (
            [0] => Team Object
                (
                    [id] => 3
                    [countStat] => 44
                )
        )
    [4] => Array
        (
            [0] => Team Object
                (
                    [id] => 5
                    [countStat] => 41
                )
        )
)

I has spend few hours working on it but couldn't get solution. Can someone please help ?

Thank you.

Looks like you want to sum team object's countStats for like ids. I've created a similar data structure, looped through it to form a temporary array that uses the id as a key, with associated amounts (to later sum - mine is amount, yours is countStats). Then sum those and recreate desired output.

<?php

class Team
{
    public $id;
    public $amount;
    public function __construct($id, $amount)
    {
        $this->id     = $id;
        $this->amount = $amount;
    }
}

$input =
[
    [new Team(1, 3)],
    [new Team(1, 4)],
    [new Team(2, 5)],
    [new Team(2, 7)]
];

foreach($input as $subarray)
    $amounts[$subarray[0]->id][]=$subarray[0]->amount;

print_r($amounts);

foreach($amounts as $k => $v)
    $result[] = [new Team($k, array_sum($v))];

print_r($result);

Output:

Array
(
    [1] => Array
        (
            [0] => 3
            [1] => 4
        )

    [2] => Array
        (
            [0] => 5
            [1] => 7
        )

)
Array
(
    [0] => Array
        (
            [0] => Team Object
                (
                    [id] => 1
                    [amount] => 7
                )

        )

    [1] => Array
        (
            [0] => Team Object
                (
                    [id] => 2
                    [amount] => 12
                )

        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM