我绞尽脑汁试图想出一个解决方案。我可以找到很多解决方案来消除二维阵列中的欺骗,但是我需要删除值低于另一个值的欺骗。这是数组:
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循环会更合适,但我不知道要测试什么条件。
请问任何想法?!
答案 0 :(得分:1)
我会遍历您的数据并创建一个字典,其中键是用户ID,值是附加运动的相应用户对象。然后,您可以通过使用运动数据循环遍历这个去掉的数组来重建您的示例数据数组结构,以确定每个用户的放置位置。
要创建去除数组,请使用以下内容:
$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...