Get the minimum value from an array that contains null values

How to get the minimum value from an array containing some null values ​​in PHP. I have such an array

array(10,20,null,60) 

Since this array contains null, so the php min function gives 0 as the minimum value, but I need, for example, a minimum of 10. Can anyone help me?

+10
arrays php
source share
3 answers

Use array_diff() to exclude null values, and after that use min() :

 $r = array_diff(array(10, 20, null, 60), array(null)); min($r); 
+15
source share

Use array_filter() as a wrapper with strlen as a callback.

 echo min(array_filter($arr,'strlen')); 

Demonstration

+3
source share

Try

  function min($array){ $min = 1000;//max value you expect for($i=0; $i<sizeof($array); $i++){ if($array[$i] < $min && $array[$i] != null) $min = $array[$i]; } return $min; } 
-one
source share

All Articles