If [Get Variable] is equal to [Array]
I am trying to find easier and simple way to code a logic.
That is if one variable is equal to any key values in an array.
For instance:
$someArray = array("a","b","c");
If($_GET["foobar"] == $someArray) {
return true;
} else {
return false;
}
If the $_GET["foobar"] had a value of A, B, or C, the case would return true. If it was any other values, it would ret开发者_如何学Pythonurn false.
Thanks for the help.
return in_array($_GET["foobar"], $someArray, true);
EDIT: Added optional true
parameter.
Rather than integer-indexed arrays, you can make use of associative arrays:
$someArray = array('a' => 1, 'b' => 1, 'c' => 1);
if (isset($someArray[$_GET['foobar']])) {
...
}
If you don't like to type out all the array values or the values of $someArray need to stay as they are, you can use array_flip
:
$someArray = array('a', 'b', 'c');
...
$otherArray = array_flip($someArray);
if (isset($otherArray[$_GET['foobar']])) {
...
}
You can even store useful information in the values of the associative array.
You can use the in_array() function. I'm pretty sure it's exactly what you are looking for. Here is the function in the code sample you provided.
$someArray = array("a","b","c");
if(in_array($_GET["foobar"],$someArray)) {
return true;
} else {
return false;
}
精彩评论