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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。