Is there a specific 开发者_开发问答way to check for a specific integer within a switch statment.
For example.
$user = $ads[$i]->from_user;
To check for the number 2 as $i in the above expression.
You can check like:
if ($ads[$i] === 2)
{
// code here
}
Or if you meant alone, you can do:
if ($i === 2)
{
// code here
}
If the number in string representation (type), you should use ==
rather than ===
.
If however you meant whether 2 is present in the array $ads
:
if (in_array(2, $ads))
{
// 2 found in $ads array
}
If i understood you right, what you want is to check if the key2
exists in$ads
.
if(array_key_exists(2, $ads)) {
// the key 2 exists in the array
}
This way, you should get the result in constant time O(1) becausearray_key_exists
is implemented with a hashtable lookup.
in_array
would require linear time O(n).
its simple, better you can use ===
operator to match your values with its datatype
精彩评论