doru52911 2016-01-19 10:36
浏览 78
已采纳

如何从此数组中查找最小和最大值

I have this array which stores the values in the array in this format

Array
(
        [0] => 0,20
        [1] => 21,50
        [2] => 201,300
        [3] => 301,400
)

now how will I find the smallest and the largest numeric value from it ?

  • 写回答

4条回答 默认 最新

  • douying3251 2016-01-19 10:43
    关注

    I think you need minimum and maximum range.

    For minimum and maximum range, first walk through array and replace

    , by . so that they become numbers (comparable).

    Them find out min() and max() of the resulting array.

    Find out the key where the elements sit.

    Now, access the elements of the original array with these keys.

    <?php
    $org = $result = Array(
            '0,20',
            '21,50',
            '201,300',
            '301,400',
    );
    
    $result = array_map(function($el) {
        return str_replace(',','.',$el); }, $result
                       );
    echo '<pre>';
    $minKey = array_search(min($result), $result);
    $maxKey = array_search(max($result), $result);
    
    $min2 = $org[$minKey]; // Returns 0,20
    $temp = explode(',', $min2);
    $min = $temp[0];
    echo $min; // Prints 0
    
    echo "<br/>";
    $max2 = $org[$maxKey]; // Returns 301,400
    $temp = explode(',', $max2);
    $max = $temp[1];
    echo $max; // Prints 400
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示