I'm gonna need a little help from you guys to fix this little code. The idea is to remove any numbers that are inside $remove_str
from $list_str
. As you can see I've already tried to solve the problem by turning both strings into arrays and simply loop through the list array searching for values inside the remove array and remove it if there's a match. However, the results are anything but what I expected. I've been toying around with it for a while now, but my head is spinning to much to see the solution.
我需要你们的帮助来修复这个小代码。我们的想法是从$ list_str中删除$ remove_str中的任何数字。正如您所看到的,我已经尝试通过将两个字符串转换为数组来解决问题,并简单地遍历列表数组,搜索remove数组中的值,如果匹配则将其删除。但是,结果不过是我的预期。我一直在玩它已经有一段时间了,但我的头脑旋转到很多,看到了解决方案。
<?php
$remove_str = '5,6,8,56,195';
$list_str = '1,3,6,9,34,150,195,213';
$remove_arr = explode(',', $remove_str);
$list_arr = explode(',', $list_str);
foreach($list_arr as $value){
$position = array_search($value, $remove_arr);
if($position !== false){
unset($list_arr[$position]);
} else {
continue;
}
}
$result = implode(',', $list_arr);
echo $result;
?>
Result:
1,6,9,150,195,213
Expected result:
1,3,9,34,150,213
1 个解决方案
#1
3
You can use array_diff,
你可以使用array_diff,
array_diff($list_arr, $remove_arr);
#1
3
You can use array_diff,
你可以使用array_diff,
array_diff($list_arr, $remove_arr);