Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find min value with out count zero in php?

Tags:

function

php

i want to find min value with out count zero form min() function in php, but it get me with zero, how can I find out with out zero?

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

the expected result which I want is to be 3, but it gives me zero,

how can i neglect zero, i want a result with out zero, please help me to do this. thanks in advance

like image 941
user2046638 Avatar asked Dec 26 '22 03:12

user2046638


2 Answers

Something like this

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
like image 182
sectus Avatar answered Jan 19 '23 00:01

sectus


Try this:

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().

like image 26
BlitZ Avatar answered Jan 19 '23 00:01

BlitZ