DxDogo DxDogo - 2 months ago 25
PHP Question

php array skip values greater than X

I have an array with values between 1-100. However, in case of an error, is there a way to make one final check just to be sure I skip/ignore values greater than 100 and the output is between 1-100?

Answer

This is where array_filter() comes in handy.

$lower_limit = 1;
$upper_limit = 100;

$array = array_filter(
    $array,
    function ($value) use ($lower_limit, $upper_limit) {
        return ($value >= $lower_limit && $value <= $upper_limit);
    }
);
Comments