Inserting and removing a string into/from an array
Posted
by alex
on Stack Overflow
See other posts from Stack Overflow
or by alex
Published on 2010-03-12T10:55:15Z
Indexed on
2010/03/12
11:17 UTC
Read the original article
Hit count: 172
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.
© Stack Overflow or respective owner