doushi1912 2012-03-26 10:04
浏览 9
已采纳

用户居中的顶级列表

I have a array with users sorted by score. I only want to output a specific users score (in the middle of the outputted list), and users before and after to make the list max length 10.

Like this

1.  A
2.  B
3.  C
--------- OUTPUT START
4.  D
5.  E
6.  F
7.  G
8.  H (ME)
9.  I
10. J
11. K
12. L
13. M
--------- OUTPUT END
14. N
15. O
16. P

Any ideas on how to do this? Thanks in advance

  • 写回答

4条回答 默认 最新

  • duanmu6231 2012-03-26 10:12
    关注

    Try that:

    $my_rank;
    foreach ($scores as $key => $value) {
      if ($value == 'H') { // 'H' being your name.
        $my_rank = $key;
      }
    }
    $surroundings = array_slice($scores, max(0, $my_rank - 4), 10);
    

    It takes the id of your name in the array and slices the array from your rank-4 with size of 10. If your rank is less than 4, it takes 0 instead (it's the max part).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭