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

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

问题描述

我最近在我的代码中使用 array_search 函数时发现了问题.我在数组$allcraftatts"中搜索值sharp".我试图通过设置两行实验来隔离问题:

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.

现在,如果我将代码更改为

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

形成 PHP 文档

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

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.

因为第一个元素是 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

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

Solution use strict option to search identical values

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

var_dump($testsharp);

输出

10

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

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