What is the best way to remove specific elements from an array in PHP?

To remove specific elements from an array in PHP, you can use the array_filter() function along with a custom callback function that specifies the elements to be removed. The callback function should return false for the elements you want to remove and true for the elements you want to keep.

// Original array
$array = [1, 2, 3, 4, 5];

// Elements to remove
$elementsToRemove = [2, 4];

// Remove specific elements from the array
$array = array_filter($array, function($value) use ($elementsToRemove) {
    return !in_array($value, $elementsToRemove);
});

print_r($array); // Output: Array ( [0] => 1 [2] => 3 [4] => 5 )