简体   繁体   English

根据更高的点从多维数组中删除重复项

[英]Remove duplicates from multi-dimensional array based on higher points

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. 我可以找到很多解决方案来从2d数组中删除重复对象,但是我需要删除值低于另一个对象的重复对象。 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. 如您所见,用户ID 2,Beans是篮球和足球的首选。 As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value. 由于他们的足球得分更高,因此我需要删除他们的篮球条目,以使ID 314变为0。

I would need to do this continually until no user be the 0 value for any of the primary array values twice. 我将需要连续执行此操作,直到没有任何用户两次将任何主数组值设为0为止。

I've tried various combinations of foreach solutions but I'm not getting anywhere. 我已经尝试过foreach解决方案的各种组合,但是我一无所获。 I thought a while loop would be more suitable but I don't know what condition to test for. 我以为使用while循环会更合适,但我不知道要测试什么条件。

Any ideas please?! 有什么想法吗?

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. 我将遍历您的数据并创建一个字典,其中的键是用户ID,值是附加了运动的适当的用户对象。 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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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