dsc862009 2015-06-24 01:27
浏览 109
已采纳

找到三个最大数字的数组(PHP)

I need to know how I can find the three greatest numbers of an array. Currently I can only find the greatest number of an array, but I want to have the three greatest of it:

$avg = array();
$avg[20,10,30,50,80,90,220];
echo max($avg);

Maybe you can help me by this?

  • 写回答

1条回答 默认 最新

  • dsn46282 2015-06-24 01:31
    关注
    // The array
    $avg = [20,10,30,50,80,90,220];
    // remove duplicates if any
    $avg = array_unique($avg);
    // sort in descending order
    rsort($avg);
    // get the first three elements
    $avg = array_slice($avg, 0, 3);
    

    Demo

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

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败