如何从数组中删除特定值的多个实例[重复]

This question already has an answer here:

I am trying to unset every instance of a specific value from my array, however I am only able to unset the first instance of that value. How can I remove everything where the value equals what I am searching for?

$toRemove = array("red","blue");
$myArray = array("red", "green", "blue", "blue", "blue", "purple", "yellow");

foreach ($toRemove as $remove)
{
    if (($key = array_search($remove, $myArray)) !== false)
    {
        unset($myArray[$key];
    }
}
print_r($myArray);

The above is only returning this as my array values:

array("green", "blue", "blue", "purple", "yellow");
</div>

Just compute the difference:

$myArray = array_diff($myArray, $toRemove);

However, your current code only returns one key so you would need to search for more. You can use a while:

foreach ($toRemove as $remove)
{
    while (($key = array_search($remove, $myArray)) !== false)
    {
        unset($myArray[$key]);
    }
}

Or get all of the keys and loop them:

foreach ($toRemove as $remove)
{
    foreach (array_keys($myArray, $remove) as $key)
    {
        unset($myArray[$key]);
    }
}