Array Filtering in PHP
You can filter arrays using the array_filter()
function, which allows you to filter elements based on a callback function.
Example of Array Filtering
<?php
$numbers = array(1, 2, 3, 4, 5);
// Filter even numbers
$even_numbers = array_filter($numbers, function($number) {
return $number % 2 == 0;
});
print_r($even_numbers);
?>
Output:
Array
(
[1] => 2
[3] => 4
)
Explanation: This example demonstrates how to filter an array using array_filter()
to get only even numbers.