The solution is quite simple: Using built-in function array_diff of PHP:

$dest_array = array_diff($src_array, array("value_to_remove_1", "value_remove_2"));

Of course you can use this method to remove just one value from array:

$dest_array = array_diff($src_array, array("value_to_remove"));

One problem of this method is that, there are some holes in $dest_array after apply this method. There are some ways of solving that problem:

  1. Remove all key strings from the array and replace them with numbers, use array_values
  2. Preserve the key names (strings), or reindex the array if all keys are numerical, use array_merge
Remove one or more values from array in PHP
Tagged on:     

Leave a Reply