array_search返回错误的密钥[重复]

时间:2021-10-26 20:18:57

This question already has an answer here:

这个问题在这里已有答案:

I have this array:

我有这个数组:

$ar = [ 'key1'=>'John', 'key2'=>0, 'key3'=>'Mary' ];

and, if I write:

如果我写:

$idx = array_search ('Mary',$ar);
echo $idx;

I get:

key2

I have searched over the net and this is not isolate problem. It seems that when an associative array contains a 0 value, array_search fails if strict parameter is not set.

我在网上搜索过,这不是隔离问题。似乎当关联数组包含0值时,如果未设置strict参数,则array_search将失败。

There are also more than one bug warnings, all rejected with motivation: “array_search() does a loose comparison by default”.

还有不止一个错误警告,所有错误都被拒绝了:“array_search()默认情况下进行松散的比较”。

Ok, I resolve my little problem using strict parameter...

好的,我使用严格的参数来解决我的小问题......

But my question is: there is a decent, valid reason why in loose comparison 'Mary'==0 or 'two'==0 or it is only another php madness?

但我的问题是:有一个体面的,有效的理由,为什么在宽松的比较'玛丽'== 0或'两个'== 0或它只是另一个PHP疯狂?

4 个解决方案

#1


9  

You need to set third parameter as true to use strict comparison. Please have a look at below explanation:

您需要将第三个参数设置为true才能使用严格比较。请看下面的解释:

array_search is using == to compare values during search

array_search使用==在搜索期间比较值

FORM PHP DOC

FORM PHP DOC

If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the haystack. This means it will also check the types of the needle in the haystack, and objects must be the same instance.

如果第三个参数strict设置为TRUE,那么array_search()函数将在haystack中搜索相同的元素。这意味着它还将检查大海捞针中的针的类型,并且对象必须是同一个实例。

Becasue the second element is 0 the string was converted to 0 during search

第二个元素是0,在搜索过程中字符串被转换为0

Simple Test

var_dump("Mary" == 0); //true
var_dump("Mary" === 0); //false

Solution use strict option to search identical values

解决方案使用严格选项来搜索相同的值

$key = array_search("Mary", $ar,true);
                                  ^---- Strict Option
var_dump($key);

Output

string(4) "key3"

#2


4  

You have a 0 (zero) numeric value in array, and array_search() perform non-strict comparison (==) by default. 0 == 'Mary' is true, you should pass 3rd parameter to array_search() (true).

数组中有0(零)数值,默认情况下array_search()执行非严格比较(==)。 0 =='Mary'为true,你应该将第3个参数传递给array_search()(true)。

#3


0  

You just chnage in your array in 'key2'=>'0' you not give single or double quotation

你只需在'key2'=>'0'中输入你的数组,你就不会给出单引号或双引号

$ar = [ 'key1'=>'John', 'key2'=>'0', 'key3'=>'Mary' ];

this is working fine

这工作正常

#4


0  

  $ar = [ 'key1'=>'John', 'key2'=>'0', 'key3'=>'Mary' ];

#1


9  

You need to set third parameter as true to use strict comparison. Please have a look at below explanation:

您需要将第三个参数设置为true才能使用严格比较。请看下面的解释:

array_search is using == to compare values during search

array_search使用==在搜索期间比较值

FORM PHP DOC

FORM PHP DOC

If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the haystack. This means it will also check the types of the needle in the haystack, and objects must be the same instance.

如果第三个参数strict设置为TRUE,那么array_search()函数将在haystack中搜索相同的元素。这意味着它还将检查大海捞针中的针的类型,并且对象必须是同一个实例。

Becasue the second element is 0 the string was converted to 0 during search

第二个元素是0,在搜索过程中字符串被转换为0

Simple Test

var_dump("Mary" == 0); //true
var_dump("Mary" === 0); //false

Solution use strict option to search identical values

解决方案使用严格选项来搜索相同的值

$key = array_search("Mary", $ar,true);
                                  ^---- Strict Option
var_dump($key);

Output

string(4) "key3"

#2


4  

You have a 0 (zero) numeric value in array, and array_search() perform non-strict comparison (==) by default. 0 == 'Mary' is true, you should pass 3rd parameter to array_search() (true).

数组中有0(零)数值,默认情况下array_search()执行非严格比较(==)。 0 =='Mary'为true,你应该将第3个参数传递给array_search()(true)。

#3


0  

You just chnage in your array in 'key2'=>'0' you not give single or double quotation

你只需在'key2'=>'0'中输入你的数组,你就不会给出单引号或双引号

$ar = [ 'key1'=>'John', 'key2'=>'0', 'key3'=>'Mary' ];

this is working fine

这工作正常

#4


0  

  $ar = [ 'key1'=>'John', 'key2'=>'0', 'key3'=>'Mary' ];