What is the difference between using a foreach loop and array_filter in PHP?

When working with arrays in PHP, a foreach loop is used to iterate through each element in the array and perform some operation on it. On the other hand, array_filter is a function that allows you to filter an array based on a specified condition and returns a new array with only the elements that meet that condition. Using array_filter can be more concise and efficient than manually looping through the array with a foreach loop and checking each element.

// Example using foreach loop
$numbers = [1, 2, 3, 4, 5];
$evenNumbers = [];
foreach ($numbers as $number) {
    if ($number % 2 == 0) {
        $evenNumbers[] = $number;
    }
}
print_r($evenNumbers);

// Example using array_filter
$numbers = [1, 2, 3, 4, 5];
$evenNumbers = array_filter($numbers, function($number) {
    return $number % 2 == 0;
});
print_r($evenNumbers);