Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MIN value from array without 0

Tags:

php

$array1 = array(0,1,3,0);
$array2 = array(2,0,3,2);
$array3 = array(0,4,5,1);

echo min($array1) . "\n";
echo min($array2) . "\n";
echo min($array3) . "\n";

this return me:

0
0
0

but i want receive:

1
2
1

How to skip 0 in function MIN?

like image 332
Peter Vorkter Avatar asked Jul 09 '12 10:07

Peter Vorkter


People also ask

How do you find the min excluding zero?

Find minimum value excluding zero with formula 1. Select a blank cell (H1) for placing the minimum value, enter formula =SMALL(A1:E7,COUNTIF($A$1:$E$7,0)+1) into the Formula Bar, and then press the Enter key.


1 Answers

Try array_filter:

If no callback is supplied, all entries of input equal to FALSE will be removed.

so it will remove 0 for you.

echo min(array_filter($array1)) . "\n";
like image 194
xdazz Avatar answered Sep 21 '22 11:09

xdazz