dsjk3214 2015-08-06 02:01
浏览 45
已采纳

在PHP中覆盖2d数组覆盖索引[重复]

This question already has an answer here:

I have a 2d array like this :

array (size=3)
  0 => 
      array (size=2)
        'player_id' => string '26' (length=2)
        'score' => float 4.702030124427
  1 => 
      array (size=2)
        'player_id' => string '27' (length=2)
        'score' => float 17.891873624039
  2 => 
      array (size=2)
        'player_id' => string '29' (length=2)
        'score' => float 17.883449353824

I'm using usort to sort it and it becomes like this:

array (size=3)
   1 => 
     array (size=2)
              'player_id' => string '27' (length=2)
              'score' => float 17.891873624039
   2 => 
     array (size=2)
              'player_id' => string '29' (length=2)
              'score' => float 17.883449353824
   0 => 
     array (size=2)
             'player_id' => string '26' (length=2)
             'score' => float 4.702030124427

my usort function is this:

  uasort($scoreList,function($a, $b) {
                            if($a['score']==$b['score']) return 0;
                                return $a['score'] < $b['score']?1:-1;
                        });

is there any way I can overwrite the indexes so the sorted list becomes 0,1,2 instead of 1,2,0?

</div>
  • 写回答

1条回答 默认 最新

  • donglin4636 2015-08-06 02:04
    关注

    You're not using usort; you're using uasort. uasort keeps keys, usort will make new keys.

    http://php.net/manual/en/function.usort.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真