What's the best way to exclude an array of values from another array? Like if I had a list of values I don't want in a different list? I'm trying not to use reg ex and I don't think it开发者_开发技巧 should be an option.
Take a look at PHP's array_diff()
-function. Example from php.net:
// original list
$array1 = array("a" => "green", "red", "blue", "red");
// these values will be removed from the first array
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
Result:
Array
(
[1] => blue
)
Use
array_dif
For example like this
// our initial array
$arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");
print_r($arr);
// remove the elements who's values are yellow or red
$arr = array_diff($arr, array("yellow", "red"));
print_r($arr);
// optionally you could reindex the array
$arr = array_values($arr);
print_r($arr);
精彩评论