Maximum and Minimum integer from array excluding NULL or ZERO values

前端 未结 2 914
旧时难觅i
旧时难觅i 2021-01-27 10:32

I am using below php functions to get highest and lowest integer value from my array.

min($output[$k]) AND max($output[$k])

Problem: I

相关标签:
2条回答
  • 2021-01-27 10:39

    Use

    max(array_filter($output[$k));
    
    0 讨论(0)
  • 2021-01-27 11:06

    Use array_diff() to filter array:

    <?php
    header('Content-Type: text/plain');
    
    $buffer   = array(null, 0, 1, 2, 3, 0, null, 1);
    $filtered = array_diff($buffer, array(null, 0));
    
    $max = max($filtered);
    $min = min($filtered);
    
    var_dump($max, $min);
    ?>
    

    Shows:

    int(3)
    int(1)
    
    0 讨论(0)
提交回复
热议问题