array_keys 如何搜索值?
PHP array_keys 如何搜索值?
例子:
$array2 = array("xyz", "xyz", "abc", "abc", "xyz", "xyz", "text", "abc", "text");
print_r(array_keys($array2,"abc"));
因为它们是键值对。我猜测 PHP 会基于哈希进行搜索,而不是迭代数组中的每个密钥对。
对此有什么“清晰的想法”吗?
受此问题启发的问题:如果类似大小的数组中的相应元素是数字(不进行迭代),如何获取数组中空元素的键?
How does PHP array_keys do the search for value?
Example:
$array2 = array("xyz", "xyz", "abc", "abc", "xyz", "xyz", "text", "abc", "text");
print_r(array_keys($array2,"abc"));
Since they are key,value pairs. I am guessing PHP to do a search based on hash rather than iterate through each of the key-pairs in the array.
Any 'clarity thoughts' on this?
Question inspired by this question: How to get the keys of empty elements in an array if the corresponding element in a similar-sized array is a number (without iteration)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 php 源代码中,它们逐一迭代每个键和值。
https://github.com/php/php -src/blob/master/ext/standard/array.c#L2439
In the php source, they iterate through each key and value, one by one.
https://github.com/php/php-src/blob/master/ext/standard/array.c#L2439
请参阅 array_keys 定义,另请注意对于解释得很好的评论:
及以后的值:
PHP 5.3 在
ext/standard/array.c
上的定义如下:array_keys
文档:另请参阅
$strict
参数来影响值的比较方式:记录了两种类型的比较 ==(相等)和 ===(相同)以及。
Please see the array_keys definition, take note also for the comments which explain it pretty well:
and later on:
The definition as follows for PHP 5.3 on
ext/standard/array.c
:The search that is performed is also explained on the PHP manual page for
array_keys
Docs:See also the
$strict
parameter to influence how values are compared:The two types of comparison == (Equal) and === (Identical) are documented as well.
我怀疑这不是那么容易:
需要遍历数组,一路将值转换为
可比较类型。
I suspect this to be not so easy:
need to walk the array, along the way converting values to a
compareable type.