How can array_filter() or array_walk() be used to remove unwanted elements from an array in PHP?

To remove unwanted elements from an array in PHP, you can use the array_filter() function to filter out elements based on a callback function that determines which elements to keep. Alternatively, you can use array_walk() to iterate over the array and remove elements that meet certain criteria. Both functions provide a flexible way to manipulate arrays and remove unwanted elements efficiently.

// Using array_filter() to remove unwanted elements from an array
$array = [1, 2, 3, 4, 5];
$unwanted = [2, 4];

$array = array_filter($array, function($value) use ($unwanted) {
    return !in_array($value, $unwanted);
});

print_r($array);