根据工资和积分排序Php数组

时间:2016-01-30 13:25:26

标签: php arrays sorting

我有一个已经按points降序排序的球员数组。就像这样:

 $positionArray = 
    Array
    (
        [PG] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [player_id] => 31471
                        [salary] => 10800
                        [points] => 51.53
                        [position] => PG
                    )

                [1] => Array
                    (
                        [id] => 3
                        [player_id] => 3223
                        [salary] => 8000
                        [points] => 49.53
                        [position] => PG
                    )

                [2] => Array
                    (
                        [id] => 5
                        [player_id] => 4356
                        [salary] => 11000
                        [points] => 45.53
                        [position] => PG
                    )
                 )
                [3] => Array
                    (
                        [id] => 4
                        [player_id] => 5654
                        [salary] => 6000
                        [points] => 42.53
                        [position] => PG
                    )
                 )
        [SF] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [player_id] => 4433
                        [salary] => 11000
                        [points] => 45.1
                        [position] => SF
                    )

                [1] => Array
                    (
                        [id] => 6
                        [player_id] => 7667
                        [salary] => 10800
                        [points] => 44
                        [position] => SF
                    )

                [2] => Array
                    (
                        [id] => 9
                        [player_id] => 4533
                        [salary] => 8000
                        [points] => 39.53
                        [position] => SF
                    )
                 )
        [SG] => Array
            (
                [0] => Array
                    (
                        [id] => 8
                        [player_id] => 3245
                        [salary] => 9000
                        [points] => 56.53
                        [position] => SG
                    )

                [1] => Array
                    (
                        [id] => 7
                        [player_id] => 7867
                        [salary] => 6000
                        [points] => 54.53
                        [position] => SG
                    )

                [2] => Array
                    (
                        [id] => 15
                        [player_id] => 4543
                        [salary] => 7000
                        [points] => 53.53
                        [position] => SG
                    )
                 )

                [2] => Array
                    (
                        [id] => 15
                        [player_id] => 2322
                        [salary] => 5000
                        [points] => 51.53
                        [position] => SG
                    )
                 )
        [PF] => Array
            (
                [0] => Array
                    (
                        [id] => 11
                        [player_id] => 7777
                        [salary] => 4000
                        [points] => 60.53
                        [position] => PF
                    )

                [1] => Array
                    (
                        [id] => 54
                        [player_id] => 1232
                        [salary] => 8000
                        [points] => 59.3
                        [position] => PF
                    )

                [2] => Array
                    (
                        [id] => 18
                        [player_id] => 5678
                        [salary] => 3000
                        [points] => 54.45
                        [position] => PF
                    )
                 )
        [C] => Array
            (
                [0] => Array
                    (
                        [id] => 23
                        [player_id] => 8906
                        [salary] => 5000
                        [points] => 47.53
                        [position] => C
                    )

                [1] => Array
                    (
                        [id] => 22
                        [player_id] => 9007
                        [salary] => 4000
                        [points] => 45.53
                        [position] => C
                    )

                [2] => Array
                    (
                        [id] => 43
                        [player_id] => 1008
                        [salary] => 11000
                        [points] => 43.53
                        [position] => C
                    )
                 )
     )

PG,SG,SF,PF,C是玩家的位置。我想要一个包含PG位置2位玩家,SF位置2位玩家,SG位置2位玩家,SF位置2位玩家的阵列C位置的一名球员。 像这样:

Array
(
    [PG_1] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => PG
          )
    [PG_2] => Array
        (

            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => PG
          )
    [SG_1] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => SG
          )
    [SG_2] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => SG
          )
    [SF_1] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => SF
          )
    [SF_2] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => SF
          )
    [PF_1] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => PF
          )
    [PF_2] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => PF
          )
    [C_1] => Array
        (
            [id] => 
            [player_id] =>
            [salary] => 
            [points] => 
            [position] => c
          )

)

这意味着我只想在最大points的基础上获得9名玩家,并且玩家salary的总和不应超过60,000的限制。

注意:播放器不应该重复,如果所有上述位置都不在数组中,只会抛出错误。这意味着我们需要所有这些位置

这是我到目前为止所做的:

 $lineupTemp = array(
         "PG",
        "PG",
        "SG",
        "SG",
        "SF",
        "SF",
        "PF",
        "PF",
        "C"
    );
$lineupCount=array_count_values($lineupTemp);

foreach ($lineupCount as $posss=>$countloop){
    if(!isset($positionArray[$posss])){
        break;
    }
        foreach ($this->positionArray[$posss] as $reco_){
            for($llll=0;$llll<$countloop;$llll++){
                        array_push($combineLinupArray,array( 
                                                                  'array_id'=>$posss.'_'.($llll + 1),
                                                                  'point'=>$this->positionArray[$posss][$llll]['points'],
                                                                  'salary'=>$this->positionArray[$posss][$llll]['salary'],
                                                                  'fantasy_id'=>$this->positionArray[$posss][$llll]['id'],
                                                                  'position'=>$posss,
                                                                  'key_used'=>$llll 
                                                               )
                                      );

             }
             break; 
          }
    }

  if(count($combineLinupArray)<9){
      //return message that all the positions are not found..
    }
After that I am adjusting the salary to iterate the array.

问题是,它耗费了太多时间,那么我该如何制作这个阵列呢?

0 个答案:

没有答案