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

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

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?