Hey all,
Does anyone know if PHP has a statement that is similar to MySqls IN() statement?
I want to do something like:
$somevar = '2,4,5,6,9'; if ('2' IN($somevar)) { //Do something.. }
Any ideas? Thanks!
I came up with this that kinda works, are there any other more elegant solutions?
$somevar = array('2','4','5','6','9'); $has_value = in_array('6', $somevar); if ($has_value == TRUE) { //Do something.. }
That's about as elegant as it gets. If you mean "less lines = more elegant", then you could just go with "if (in_array('6', $somevar)) {". (Which is the way I'd do it.)
I agree with the above. If it had to be a csv string this is as simple as you could get
function in_csv($needle, $haystack) { return in_array($needle, explode(',', $haystack)); }
Good example, D. Now that's elegance.
Rockin all! thanks for the feedback!