Filtering an Array in PHP


Filter elements of an array using a callback function with array_filter().

Source Code

$numbers = [1, 2, 3, 4, 5, 6];
$even = array_filter($numbers, function($num) { return $num % 2 == 0; });
print_r($even); // Keeps only the even elements
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments