Remove one or more values from array in PHP
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:
- Remove all key strings from the array and replace them with numbers, use
array_values
- Preserve the key names (strings), or reindex the array if all keys are numerical, use
array_merge