从另一个数组中排除值数组的最佳方法是什么?就像我有一个值列表我不想要在不同的列表中?我试图不使用reg ex,我认为它不应该是一个选项。
答案 0 :(得分:1)
看看PHP的array_diff()
-function。来自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);
结果:
Array
(
[1] => blue
)
答案 1 :(得分:0)
使用
array_dif
例如像这样
// 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);