因此,我们的想法是从一个数组中删除其他数组的编号列表,但仅删除第一个数组,解决该问题的任何帮助将不胜感激。
$my_array = array("Sandra Loor,593984076233","Adrian,593998016010","Lucas,593999843424");
function myFilter($string) {
$to_remove = array("593984076233","593998016010");
foreach($to_remove as $remove) {
return strpos($string, $remove) === false;
}
}
$newArray = array_filter($my_array, 'myFilter');
foreach ($newArray as $val){
echo $val.'<br>';
}
答案 0 :(得分:2)
问题是您的过滤器将始终从循环的第一次迭代开始return
。
相反,如果找到该条目,它将返回false,并在循环结束时返回true(即完全找不到)...
function myFilter($string) {
$to_remove = array("593984076233","593998016010");
foreach($to_remove as $remove) {
if ( strpos($string, $remove) !== false )
return false;
}
return true;
}
答案 1 :(得分:2)
另一种方法是使用preg_grep在数组中搜索要删除的项目,然后使用array_diff删除它们。
$my_array = array("Sandra Loor,593984076233","Adrian,593998016010","Lucas,593999843424");
$to_remove = array("593984076233","593998016010");
$to_remove = "/" . implode("|", $to_remove) . "/";
$match = preg_grep($to_remove, $my_array);
$filtered = array_diff($my_array, $match);
var_dump($filtered);
// Lucas,593999843424
您还可以像这样将其捆扎成一个衬板:
$my_array = array("Sandra Loor,593984076233","Adrian,593998016010","Lucas,593999843424");
$to_remove = array("593984076233","593998016010");
$filtered = array_diff($my_array, preg_grep("/" . implode("|", $to_remove) . "/", $my_array));
var_dump($filtered);