Checking If All Array Elements Pass a Test in PHP


Utilize array_filter() to apply a callback to each element; handy for conditional checks.

Source Code

$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, function($number) {
    return $number % 2 === 0;
});
print_r($even); // Outputs: Array ( [1] => 2 [3] => 4 )
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments