tags:

views:

276

answers:

1

Hi there,

I'm racking my brains trying to think of a solution. I can find plenty of solutions to remove dupes from a 2d array but I need to remove dupes where a value is lower than the other. Here is the array:

Array
(
    [basketball] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 2
                    [username] => Beans
                    [points] => 30
                )

            [1] => stdClass Object
                (
                    [id] => 314
                    [username] => slights
                    [points] => 20
                )

            [2] => stdClass Object
                (
                    [id] => 123
                    [username] => gibb54
                    [points] => 5
                )

        )

    [soccer] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 2
                    [username] => Beans
                    [points] => 95
                )

            [1] => stdClass Object
                (
                    [id] => 49
                    [username] => sans
                    [points] => 65
                )

            [2] => stdClass Object
                (
                    [id] => 122
                    [username] => peano
                    [points] => 50
                )

            [3] => stdClass Object
                (
                    [id] => 174
                    [username] => fordb
                    [points] => 30
                )

            [4] => stdClass Object
                (
                    [id] => 112
                    [username] => danc
                    [points] => 30
                )


        )

)

As you may see, user ID 2, Beans is the first selection for both basketball and soccer. As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value.

I would need to do this continually until no user be the 0 value for any of the primary array values twice.

I've tried various combinations of foreach solutions but I'm not getting anywhere. I thought a while loop would be more suitable but I don't know what condition to test for.

Any ideas please?!

+1  A: 

I would loop through your data and create a dictionary where the keys are the user ids, and the values are the appropriate user objects with the sport appended. Then you can reconstruct your example data array structure by looping through this de-duped array using the sport data to determine where to put each user.

To create the de-duped array, use something like:

$deDupedData = array();
foreach ($data as $sport => $users) {
    foreach ($users as $user) {
        if (isset($deDupedData[$user->id])) {
            if ($user->points > $deDupedData[$user->id]->points) {
                $deDupedData[$user->id]->sport = $sport;
                $deDupedData[$user->id]->points = $user->points;
            } 
        } else {
            $modifiedUser = $user;
            $modifiedUser->sport = $sport;
            $deDupedData[$user->id] = $modifiedUser;
        }
    }
}  
// Now reconstruct your array...
DavidWinterbottom