Inserting and removing a string into/from an array
- by alex
Hello all!
I have an array and variable.
If the variable does not exist in the array it has to be added, otherwise it has to be removed.
Why the following code does not work?
$ar = ["a","b","c"];
$vr = "b";
foreach ($ar as $i => $value) {
if ($value == $vr) {
unset ($ar[$i]);
} else {
$ar[] = $vr;
$ar = array_unique($ar);
}
}
Thanks.