首页 > 解决方案 > 数组试图获得第一,我和一个高于/低于优化

问题描述

我有包含联盟中数十人的数据,并且我试图为每个联盟获得 4 个结果集。如果我在前 4 名,那么它会返回前 4 名,但如果我不是,那么它应该返回第 1 名,以及我上方和下方的一个,以及我。

目前我有一个第一个到第四个参数,我填充,然后被添加到返回的数组中。目前,此代码正在运行,但是,如果联盟中少于 4 个,则它不正确,并且如果我在前 4 名中也不起作用。

我更关心的是,我有一个很大的笨拙 if else if 块,必须有一个比我所做的更整洁/更好的方法?我错过了什么吗?

输入数据:

array(35) {
  [0]=>
  array(11) {
    ["groupID"]=>
    int(1)
    ["groupName"]=>
    string(6) "A Group"
    ["driverID"]=>
    int(1)
    ["screenName"]=>
    string(12) "MrBlog"
    ["score"]=>
    int(9231)
    ["position"]=>
    float(1)
  },
{
    ["groupID"]=>
    int(1)
    ["groupName"]=>
    string(6) "A Group"
    ["driverID"]=>
    int(2)
    ["screenName"]=>
    string(12) "Saxon"
    ["score"]=>
    int(7276)
    ["position"]=>
    float(2)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(10)
    ["screenName"]=>
    string(12) "Anonymous"
    ["score"]=>
    int(9897)
    ["position"]=>
    float(1)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(17)
    ["screenName"]=>
    string(12) "Joe"
    ["score"]=>
    int(9777)
    ["position"]=>
    float(2)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(7)
    ["screenName"]=>
    string(12) "Wavey Davey"
    ["score"]=>
    int(9564)
    ["position"]=>
    float(3)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(29)
    ["screenName"]=>
    string(12) "White Van Man"
    ["score"]=>
    int(9432)
    ["position"]=>
    float(4)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(4)
    ["screenName"]=>
    string(12) "Dan"
    ["score"]=>
    int(9278)
    ["position"]=>
    float(5)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(1)
    ["screenName"]=>
    string(12) "MrBlog"
    ["score"]=>
    int(9231)
    ["position"]=>
    float(6)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(24)
    ["screenName"]=>
    string(12) "Big Dan"
    ["score"]=>
    int(8742)
    ["position"]=>
    float(7)
  },
{
    ["groupID"]=>
    int(4)
    ["groupName"]=>
    string(6) "All Drivers"
    ["driverID"]=>
    int(2)
    ["screenName"]=>
    string(12) "Saxon"
    ["score"]=>
    int(7276)
    ["position"]=>
    float(8)
  }

期望的输出:

"1": {
        "members": 2,
        "groupName": "A Group",
        "score": [
            {
                "groupID": 1,
                "driverID": 1,
                "screenName": "MrBlog",
                "score": 9231,
                "position": 1
            },
            {
                "groupID": 1,
                "driverID": 2,
                "screenName": "Saxon",
                "score": 7276,
                "position": 2
            }
        ]
    },
"4": {
        "members": 7,
        "groupName": "All Drivers",
        "score": [
            {
                "groupID": 4,
                "driverID": 10,
                "screenName": "Anonymous",
                "score": 9897,
                "position": 1
            },
            {
                "groupID": 4,
                "driverID": 17,
                "screenName": "Joe",
                "score": 9777,
                "position": 2
            },
            {
                "groupID": 4,
                "driverID": 7,
                "screenName": "Wavey Davey",
                "score": 9564,
                "position": 3
            },
            {
                "groupID": 4,
                "driverID": 29,
                "screenName": "White Van Man",
                "score": 9432,
                "position": 4
            },
            {
                "groupID": 4,
                "driverID": 4,
                "screenName": "Dan",
                "score": 9278,
                "position": 5
            },
            {
                "groupID": 4,
                "driverID": 1,
                "screenName": "MrBlog",
                "score": 9231,
                "position": 6
            },
            {
                "groupID": 4,
                "driverID": 24,
                "screenName": "Big Dan",
                "score": 8742,
                "position": 7
            },
            {
                "groupID": 4,
                "driverID": 2,
                "screenName": "Saxon",
                "score": 7276,
                "position": 8
            }       
        ]       
    }

代码:

foreach ($leagues as $aLeagueID => $aLeague) {
        $return[$aLeague['groupID']]['members'] = $aLeague['position'];
        // New group ID?
        if ($groupID != $aLeague['groupID']) {
            // get our position
            if($aLeague['_driverID'] == $driverID)
                $myPos = $aLeague['position'];

            $return[$aLeague['groupID']]['groupName'] = $aLeague['groupName'];
            $return[$aLeague['groupID']]['score'][] = $aLeague;

        } else if ($groupID == $aLeague['groupID']) {
            if($aLeague['_driverID'] == $driverID)
                $myPos = $aLeague['position'];

            if ($stop) {
                $fourth = $aLeague;
                $return[$aLeague['groupID']]['score'][] = $first;
                $return[$aLeague['groupID']]['score'][] = $second;
                $return[$aLeague['groupID']]['score'][] = $third;
                $return[$aLeague['groupID']]['score'][] = $fourth;
                $stop = false;
                $myPos = 0;
            }

            // If we dont know our position yet
            else if($myPos == 0 && $aLeague['position'] <= 4) {
                $second = $third;
                $third = $fourth;
                $fourth = $aLeague;
                #$return[$aLeague['groupID']]['score'][] = $prev;
                #$return[$aLeague['groupID']]['score'][] = $aLeague;
            }

            // IF we are in the top 4
            else if($myPos <= 4 && $aLeague['position'] <= 4) {
                $second = $third;
                $third = $fourth;
                $fourth = $aLeague;
            }

            // else show first place and our position + / - 1
            else if($myPos >= 5) {
                $stop = true;
                $second = $prev;
                $third = $aLeague;
            }
            $prev = $aLeague;
        }
        $groupID = $aLeague['groupID'];
    }

标签: phplogic

解决方案


好的,这就是我最终的结果。涵盖了大多数情况,虽然它不是最漂亮的解决方案,但希望有一些更优雅的东西。

    foreach ($leagues as $aLeagueID => $aLeague) {
        $return[$aLeague['groupID']]['members'] = $aLeague['position'];
        // get our position
        if($aLeague['_driverID'] == $driverID && $found == false && $aLeague['globalUserID'] != 4999) {
            $myPos = $aLeague['position'];
            $found = true;
        }
        // New group ID / First position
        if ($groupID != $aLeague['groupID']) {
            $return[$aLeague['groupID']]['groupName'] = $aLeague['groupName'];
            $return[$aLeague['groupID']]['score'][] = $aLeague;
            $found = false;
            $fisrtID = $aLeagueID;
        } else if ($groupID == $aLeague['groupID']) {
            // If we dont know our position yet
            if($myPos == 0 && $aLeague['position'] <= 4) {
                // do nothing
            }
            // IF we are in the top 4
            else if($myPos <= 4 && $aLeague['position'] <= 4) {
                $return[$aLeague['groupID']]['score'][] = $leagues[$fisrtID + 1];
                if($leagues[$fisrtID + 2]['groupID'] == $groupID)
                    $return[$aLeague['groupID']]['score'][] = $leagues[$fisrtID + 2];
                if($leagues[$fisrtID + 3]['groupID'] == $groupID)
                    $return[$aLeague['groupID']]['score'][] = $leagues[$fisrtID + 3];
                $myPos = 0;
            }
            // else show first place and our position + / - 1
            else if($myPos >= 5) {
                // If we are at the end of the road! (last) then get the previous 2 records before us
                if($leagues[$aLeagueID + 1]['groupID'] != $groupID && $leagues[$aLeagueID]['groupID'] == $groupID)
                    $return[$aLeague['groupID']]['score'][] = $leagues[$aLeagueID - 2];
                $return[$aLeague['groupID']]['score'][] = $leagues[$aLeagueID - 1];
                $return[$aLeague['groupID']]['score'][] = $aLeague;
                if($leagues[$aLeagueID + 1]['groupID'] == $groupID)
                    $return[$aLeague['groupID']]['score'][] = $leagues[$aLeagueID + 1];
                $myPos = 0;
            }
        }
        // Current group ID
        $groupID = $aLeague['groupID'];
    }

推荐阅读