我用过的东西:
$maxpoints = 40;
for($i = 0; $i < count($trainergross); $i++){
$trainergross[$i]['points'] = $maxpoints;
$maxpoints -=2;
}
继承我的排序功能:
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
}
这很棘手(至少对我而言)。我需要根据最高值降级2来生成积分系统。
这是我的数组运行函数后按最高毛($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
)
)
在此示例中,$trainergross[0]
应获得key => value
"points" => 40
$trainergross[1]
会得到38 .. [2]会得到36 ......等等。
然后我会按照萌芽对数组进行操作,然后再根据最高芽增加值键。
我的问题是,我甚至不知道从哪里开始这样做。是否可以根据数组的顺序添加一个键?
我唯一的想法是做3个不同的数组,然后将它们全部合并,但这看起来非常低效。
抱歉模糊,甚至不知道怎么问,希望这解释得很好。
答案 0 :(得分:2)
如果您的数组已经排序,那么您可以使用以下内容:
$maxpoints = 40;
foreach($trainergross as $trainer){
$trainer['points'] = $maxpoints;
$maxpoints -=2;
}
如果你想根据他们在另一个标准中的位置添加更多的点,那么将数组排序为下一个属性(即芽)并重复上述内容,但是添加到当前点的值:
$trainer['points'] += $maxpoints;