Here's array_filter (), which returns a subset of this array based on the return value of this callback. If the subset is empty, then it will be the equivalent of Some () returning false, and if it is not empty, it will correspond to Some () returning true.
$unfiltered = [1, 11, 2, 22, 3, 33, 4, 44, 5, 55]; $filtered = array_filter ($unfiltered, function ($elem){ return $elem > 10; }); print_r ($unfiltered); print_r ($filtered); var_dump (empty ($filtered));
However, this approach is not short-circuited, and performance will be inversely proportional to the size of the array. However, this should not matter in the real world, because the array should still become quite huge, or array_filter is called many times before you notice a performance impact.
If performance is of the utmost importance, you will need to compile the array yourself and exit the loop as soon as you find a match.
$biggerThanTen = false; foreach ($unfiltered as $elem) { if ($elem > 10) { $biggerThanTen = true; break; } }
Gordonm
source share