从没有0的数组中获取最小值

时间:2022-05-25 22:55:40

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

如何在函数MIN中跳过0 ?

2 个解决方案

#1


13  

Try array_filter:

试试array_filter:

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

如果没有提供回调,则将删除所有等于FALSE的输入项。

so it will remove 0 for you.

它将为你移除0。

echo min(array_filter($array1)) . "\n";

#2


7  

In addition to array_filter, you can be more explicit with

除了array_filter之外,您还可以使用更显式的

$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

#1


13  

Try array_filter:

试试array_filter:

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

如果没有提供回调,则将删除所有等于FALSE的输入项。

so it will remove 0 for you.

它将为你移除0。

echo min(array_filter($array1)) . "\n";

#2


7  

In addition to array_filter, you can be more explicit with

除了array_filter之外,您还可以使用更显式的

$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