douhuan9289 2018-02-24 10:32
浏览 98
已采纳

在数组PHP中查找最高值的位置

So i want to find the position of the highest number in this array.

$numbers = array($n1, $n2, $n3, $n4, $n5, $n6, $n7, $n8, $n9, $n10);

How do i do that? Thanks

  • 写回答

1条回答 默认 最新

  • dp7311 2018-02-24 10:34
    关注

    max() can receive an array as a parameter, so with:

    $numbers = array($n1, $n2, $n3, $n4, $n5, $n6, $n7, $n8, $n9, $n10);
    $max = max($numbers);
    

    $max will have the highest number. Then, use array_search() to get the position:

    $pos = array_search($max, $numbers);
    echo "Position: ".$pos; // Position: 5
    

    Demo

    Note that the position will be the index, so if you want the "real" position, subtract one.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部