dongrandi8411 2016-09-26 22:15
浏览 9

php中的每行排名值

Is there a way to rank this row of multiple values from mysql using php

row1- (A,B,C) |  (100,200,39)
row2- (A,B,C)  |  (467,29,89)

and wanting the results to look like this

  row1
      rank
  A - 2nd
  B - 1st
  C - 3rd

  row2
      rank
  A  - 1st
  B  - 3rd
  C  - 2nd

Please any ideas as to how to do this? Thanks

  • 写回答

1条回答 默认 最新

  • dpw70180 2016-09-26 23:09
    关注

    Assuming the array of the mysql looks like this:

    $array1=array('A'=>100, 'B'=>200, 'C'=>39);
    $array2=array('A'=>467, 'B'=>29, 'C'=>89);
    

    Use arsort:

    arsort($array1);
    arsort($array2);
    

    The second parameter is: sort_flags

    Which uses these types:

    SORT_REGULAR - compare items normally (don't change types)

    SORT_NUMERIC - compare items numerically

    SORT_STRING - compare items as strings

    SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()

    SORT_NATURAL - compare items as strings using "natural ordering" like natsort()

    SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

    So you could use arsort($array1,SORT_NUMERIC);

    After sorting it, you can loop through it like this:

    $x=1;
    foreach ($array1 as $key => $val) {
        echo $key.' - '.$x;
        $x++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示