Use min () and max () of an array with php

1

The following example saves me in the variable $ min the smallest number in the list that is in the array

$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
$min=min($array); 
echo"$min";

My question: What I need is for me to look for the smallest number after 1, so I can indicate the variable $ min that the smallest number is 1 and not 0 as it is the previous case. I hope you understand. Thank you very much.

    
asked by juan pablo 09.09.2018 в 23:36
source

1 answer

1

Using array_filter you can get an array that excludes all elements less than 1. Then, you only use min with that array.

$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
$min = min(array_filter($array, function($item) {
    return $item > 0;
}));
echo $min;
    
answered by 10.09.2018 / 00:28
source