Search code examples
phparraysfilteringarray-differenceblacklist

Remove elements from a flat array using a flat blacklist array


So I have two arrays:

$one = array('red', 'green', 'blue', 'yellow', 'white');
$two = array('white', 'blue', 'red');

This being said, I need to now remove the elements from the first array that are existent in the second one. In short, the output after the sorting has to be (in this case): ['green', 'yellow'].

I've looked at the array functions at PHP's documentation but was unable to find what I need. I'm sure it's something basic but I can't recall a function for that.


Solution

  • Try array_diff()

    Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

    For example...

    $three = array_diff($one, $two);
    

    Demo ~ https://eval.in/167872