doz22551 2013-07-26 17:13
浏览 10
已采纳

使用基于数组当前位置生成的值向数组添加键

What i used:

        $maxpoints = 40;
        for($i = 0; $i < count($trainergross); $i++){
                $trainergross[$i]['points'] = $maxpoints;
                $maxpoints -=2;
        }

heres my sort function:

function grossSort($gross, $compare) {
    if($gross['gross'] > $compare['gross'])
        return -1; // move up
    else if($gross['gross'] < $compare['gross'])
        return 1; // move down
    else
        return 0; // do nothing
}

This is tricky (for me at least). I need to generate a points system based on the highest value gross degrading by 2.

here is my array after running a function to sort it by highest gross ($trainergross):

Array
(
    [0] => Array
        (
            [instr] => blah
            [club] => Colvin
            [gross] => 2146
            [bud] => 0
            [ratio] => 0
        )

    [1] => Array
        (
            [instr] => rob
            [club] => Evans
            [gross] => 2000
            [bud] => 0
            [ratio] => 0
        )

    [2] => Array
        (
            [instr] => new
            [club] => Boulevard
            [gross] => 930
            [bud] => 755
            [ratio] => 248
        )

    [3] => Array
        (
            [instr] => test
            [club] => Boulevard
            [gross] => 805
            [bud] => 50
            [ratio] => 50
        )

    [4] => Array
        (
            [instr] => lee
            [club] => Boulevard
            [gross] => 235
            [bud] => 60
            [ratio] => 30
        )

    [5] => Array
        (
            [instr] => rob
            [club] => Boulevard
            [gross] => 175
            [bud] => 125
            [ratio] => 125
        )

    [6] => Array
        (
            [instr] => rob
            [club] => Henrietta
            [gross] => 60
            [bud] => 0
            [ratio] => 0
        )

)

In this example $trainergross[0] should get a key => value of "points" => 40

$trainergross[1] would get 38.. [2] would get 36... etc.

I would then usort the array by bud, then do it again increasing the value key based on highest bud.

My issue is, i dont even know where to start with doing this. Is it possible to add a key based on the order of the array?

The only thought i have is doing 3 different arrays then merging them all, but that seems terribly inefficient.

Sorry for vague, dont really even know how to ask, hopefully this explained it well enough.

  • 写回答

1条回答 默认 最新

  • doumanni3501 2013-07-26 17:24
    关注

    If your array is already sorted then you can use something like this:

    $maxpoints = 40;
    foreach($trainergross as $trainer){
        $trainer['points'] = $maxpoints;
        $maxpoints -=2;
    }
    

    If you want to add more points based on their position in another criteria, then sort the array for next property (i.e. bud) and repeat the above but adding to the current value of points:

    $trainer['points'] += $maxpoints;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探