doukefu1361
doukefu1361
2009-11-01 14:10
浏览 42
已采纳

基于更高的点从多维数组中删除重复项

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条回答 默认 最新

  • dongyu6276
    dongyu6276 2009-11-01 19:51
    已采纳

    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...
    
    点赞 评论

相关推荐