Compare string and 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 10:57 UTC
Read the original article Hit count: 144

Filed under:

Hello all! I have an array and variable. If the variable does not exist in the array to be added, if there is - remove from the array. Why 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

Related posts about php