What is the purpose of using array_filter and array_diff in PHP when dealing with arrays?

When dealing with arrays in PHP, you may need to filter out certain elements based on a specific condition or find the difference between two arrays. This is where functions like array_filter and array_diff come in handy. array_filter is used to iterate over an array and return only the elements that satisfy a given condition, while array_diff is used to find the difference between two arrays by comparing their values.

// Using array_filter to filter out odd numbers from an array
$numbers = [1, 2, 3, 4, 5, 6];
$filteredNumbers = array_filter($numbers, function($num) {
    return $num % 2 == 0;
});

print_r($filteredNumbers);

// Using array_diff to find the difference between two arrays
$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$diff = array_diff($array1, $array2);

print_r($diff);