How to find the minimum value without zero in php?

I want to find the minimum value using the function count (0) min min () in php, but I need to get zero, how can I find out with a zero value?

$a = 0; $b = 3; $c= 4, $d = 8; $minvalue = min($a,$b,$c,$d); 

the expected result I want should be 3, but it gives me zero,

how can I neglect zero, I want to get a result with a zero value, please help me do this. thank you in advance

+4
source share
4 answers

Something like that

 function ownMin($value) { return min(array_filter(func_get_args())); } $a = 0; $b = 3; $c= 4; $d = 8; echo ownMin($a,$b,$c,$d); // 3 
+7
source

Try the following:

 function my_min(){ $excludes = array(0); // anything, that should be filtered out. $values = array_diff(func_get_args(), $excludes); return min($values); } var_dump(my_min(12, 0, 15, 0, 18)); 

Shows:

 int(12) 

Take a look at array_diff() and func_get_args() .

+2
source

try it

 function nonzero($a){ return ($a > 0); } min(array_filter($yournumbers,"nonzero")); 
+1
source

Write a function:

 function myMin() { $numargs = func_num_args(); $arg_list = func_get_args(); $min = PHP_INT_MAX ; for ($i = 0; $i < $numargs; $i++) { if($arg_list[$i] != 0 && $arg_list[$i] < $min) { $min = Arg_list[$i]; } } return $min; } 

call: mymin (0,1,2,3,5) => 1

0
source

Source: https://habr.com/ru/post/1480630/


All Articles