我绞尽脑汁想出一个解决办法。我可以找到很多解决方案来删除二维数组中的重复项,但我需要删除其中一个值低于另一个值的重复项。下面是数组:
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
)
)
)如您所见,用户ID 2,Beans是篮球和足球的第一选择。因为他们有更多的足球积分,我需要删除他们的篮球条目,使ID为314,轻视0值。
我需要不断地这样做,直到没有用户为任何主数组值的0值为止。
我尝试了foreach解决方案的各种组合,但一无所获。我认为while循环会更合适,但我不知道要测试什么条件。
有什么想法吗?!
发布于 2009-11-02 03:51:45
我将遍历您的数据并创建一个字典,其中键是用户ids,值是附加了sport的适当用户对象。然后,您可以使用sport数据遍历这个已消除重复数据的数组,以确定放置每个用户的位置,从而重新构建示例数据数组结构。
要创建已消除重复数据的阵列,请使用以下命令:
$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...https://stackoverflow.com/questions/1657255
复制相似问题