How can array functions like array_filter be used to optimize the process of finding multiples in PHP?

To optimize the process of finding multiples in PHP, we can use the array_filter function to efficiently filter out elements that are not multiples of a given number. This allows us to quickly identify and work with only the elements that meet our criteria, saving time and resources in the process.

// Find multiples of 3 in an array
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$multiplesOfThree = array_filter($numbers, function($num) {
    return $num % 3 === 0;
});

print_r($multiplesOfThree);