Peter Vorkter
Peter Vorkter

Reputation: 93

Get MIN value from array without 0

$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?

Upvotes: 6

Views: 5219

Answers (2)

Jon
Jon

Reputation: 437654

In addition to array_filter, you can be more explicit with

$min = min(array_diff($array1, array(0));    // result = 1

This can let you disregard more values very easily, e.g.

$min = min(array_diff($array1, array(0, 1)); // result = 3

Upvotes: 8

xdazz
xdazz

Reputation: 160923

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";

Upvotes: 15

Related Questions