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

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.

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;