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 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...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站