Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查数组中是否存在特定的整数?_Php - Fatal编程技术网

Php 如何检查数组中是否存在特定的整数?

Php 如何检查数组中是否存在特定的整数?,php,Php,是否有特定的方法检查开关状态中的特定整数 比如说 $user = $ads[$i]->from_user; 要在上面的表达式中将数字2作为$i进行检查。您可以通过以下方式进行检查: if ($ads[$i] === 2) { // code here } 或者,如果你是指独自一人,你可以: if ($i === 2) { // code here } 如果数字以字符串表示(类型),则应使用==而不是== 但是,如果您的意思是数组中是否存在2$ads: if (in_array(2

是否有特定的方法检查开关状态中的特定整数

比如说

$user = $ads[$i]->from_user;
要在上面的表达式中将数字2作为$i进行检查。

您可以通过以下方式进行检查:

if ($ads[$i] === 2)
{
 // code here
}
或者,如果你是指独自一人,你可以:

if ($i === 2)
{
 // code here
}
如果数字以字符串表示(类型),则应使用
==
而不是
==

但是,如果您的意思是数组中是否存在2
$ads

if (in_array(2, $ads))
{
 // 2 found in $ads array
}
您可以通过以下方式进行检查:

if ($ads[$i] === 2)
{
 // code here
}
或者,如果你是指独自一人,你可以:

if ($i === 2)
{
 // code here
}
如果数字以字符串表示(类型),则应使用
==
而不是
==

但是,如果您的意思是数组中是否存在2
$ads

if (in_array(2, $ads))
{
 // 2 found in $ads array
}

如果我没听错,您需要检查
2
键是否存在于
$ads

if(array_key_exists(2, $ads)) {
    // the key 2 exists in the array
}
通过这种方式,您应该在常量时间O(1)中获得结果,因为
array\u key\u exists
是通过哈希表查找实现的


在_数组中
需要线性时间O(n)

如果我没听错,您需要检查
$ads
中是否存在键
2

if(array_key_exists(2, $ads)) {
    // the key 2 exists in the array
}
通过这种方式,您应该在常量时间O(1)中获得结果,因为
array\u key\u exists
是通过哈希表查找实现的


在数组中
需要线性时间O(n)

它很简单,你最好使用
==
操作符将你的值与其数据类型匹配

它很简单,你最好使用
==
操作符将你的值与其数据类型匹配

你的问题标题很混乱(在阅读了实际问题之后).你的问题标题令人困惑(在阅读了实际问题后)。这是你的意思吗?这是你的意思吗?