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.
<?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