开发者

How can i check to see if a specific integer is within array?

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 key2exists 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_existsis implemented with a hashtable lookup.

in_arraywould require linear time O(n).


its simple, better you can use === operator to match your values with its datatype

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜