I have been struggling with array_search for a bit and although I think I understand it now, I just want to make absolutely sure I understand the logic behind the way my code is executing.
I am trying to write a function that will add an element to an array if it is not in the array to begin with, and remove it if it is. Simple, right?
$k = array_search($needle, $haystack)
if ( $k === FALSE ) {
$haystack[] = $needle;
} else {
unset($haystack[$k]);
}
Is this the most efficient way to write this? It seems like there should be a way to assign the value of $k and at the same time check whether its value is FALSE or anything else (including 0)?
;on first line