PHP array_search一贯返回数组的第一个关键 [英] PHP array_search consistently returns first key of array

查看:135
本文介绍了PHP array_search一贯返回数组的第一个关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在code。使用array_search功能时,注意到最近的麻烦。我正在寻找数组$ allcraftatts的值犀利。我试图孤立通过建立一个两线实验中的问题:

I recently noticed trouble when using the array_search function in my code. I am searching the array "$allcraftatts" for the value "sharp". I tried to isolate the problem by setting up a two line experiment:

$testcopy=$allcraftatts;
$testsharp=array_search("sharp", $testcopy);

使用的print_r(get_defined_vars());后来,我得到这样的结果:

Using "print_r(get_defined_vars());" later on, I get this result:

[testcopy] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                    [6] => Sharp Stone
                    [7] => Sharp Stones
                    [8] => stone
                    [9] => object
                    [10] => sharp
                    [11] => hard
                    [12] => 0
                    [13] => 0
                    [14] => 0
                    [15] => 0
                    [16] => 0
                    [17] => 0
                    [18] => 0
                )

[testsharp] => 0

我确信,我没有在其他任何时间修改这些变量。

I made sure that I do not modify these variables at any other time.

现在,如果我改变我的code到

Now, if I change my code to

$testcopy=$allcraftatts;
unset($testcopy[0]);
$testsharp=array_search("sharp", $testcopy);

返回1。

这使我相信它总是返回数组中的第一个关键。

This leads me to believe that it always returns the first key in the array.

这令我感到困惑!这是一个让你害怕什么毛病语言本身这些错误之一。然而令人怀疑这是,我居然最终驱动到的东西PHP源找错了,但遗憾的是无法理解。

It baffles me! This is one of those bugs that makes you fear something wrong with the language itself. However doubtful this is, I actually was eventually driven to looking at the PHP source for something wrong there, but unfortunately could not understand it.

看到,这是一个简单的功能,因为这,我肯定会被不可避免地简单的回答完全被羞辱,但在这一点上,我只想要一个答案。

Seeing that it is a function simple as this, I will most definitely be completely humiliated by the inevitably simple answer, but at this point, I just want an answer.

推荐答案

array_search 使用 == 比较搜索过程中的值

array_search is using == to compare values during search

FORM PHP DOC

FORM PHP DOC

如果严格第三个参数被设置为真,那么array_search()函数将搜索在草堆的相同元件。这意味着它也将检查类型大海捞针的,和对象必须是相同的实例。

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.

Becasue的第一个元素是 0 字符串被搜索时转换为 0

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

简单测试

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

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

$testsharp = array_search("sharp", $testcopy,true);
                                               ^---- Strict Option

var_dump($testsharp);

输出

10

这篇关于PHP array_search一贯返回数组的第一个关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆