php 哪个是最好的 array_search 或 in_array?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4518404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:17:23  来源:igfitidea点击:

which is best array_search or in_array?

phparrays

提问by Thilak

I have a large while loop function, every time it gets loaded for check with current URLname. So I need to know which one is better to check the URLname in large array within the while loop, in_array()or array_search()function.

我有一个很大的 while 循环函数,每次加载它以检查当前的URL名称。所以我需要知道在while循环或函数中检查大数组中的URL名称 哪个更好。in_array()array_search()

回答by mario

If it's a large array andin a loop, neither is "best". Instead use array_flip()on your array, so urls become keys. And use isset()to check for the presence.

如果它是一个大数组并且处于循环中,则两者都不是“最佳”。而是array_flip()在您的阵列上使用,因此 url 成为键。并用于isset()检查是否存在。

回答by Patrick Fisher

There's no real answer here. So I tried it, myself.

这里没有真正的答案。所以我自己试了一下。

$haystack = array
(
    'apple',
    'banana',
    'cherry',
    'lemon',
    'lime',
    'orange',
    'potato',
    'rutabaga'
);
$haySize = count($haystack);

$loops = isset( $_SERVER['argv'][1] ) ? $_SERVER['argv'][1] : 10000;
// echo 'Loops: ' . $loops . "\n";

$start = microtime(true);
for ($i = 0; $i < $loops; $i++)
{
    $needle = $haystack[ $i % $haySize ];
}
$zeroTime = microtime(true) - $start;
// echo sprintf('%0.3f', $zeroTime * 1000) . ' ms : zero time' . "\n";

$start = microtime(true);
for ($i = 0; $i < $loops; $i++)
{
    $needle = $haystack[ $i % $haySize ];
    $dummy = array_search($needle, $haystack);
}
echo sprintf('%0.3f', (microtime(true) - $start - $zeroTime) * 1000) . ' ms : array_search' . "\n";

$start = microtime(true);
for ($i = 0; $i < $loops; $i++)
{
    $needle = $haystack[ $i % $haySize ];
    $dummy = in_array($needle, $haystack);
}
echo sprintf('%0.3f', (microtime(true) - $start - $zeroTime) * 1000) . ' ms : in_array' . "\n";
    echo sprintf('%0.3f', (microtime(true) - $start) * 1000).' ms : in_array'."\n";

For a typical use case, in_array wins, but the difference is negligible:

对于典型用例, in_array 胜出,但差异可以忽略不计:

22.662 ms : array_search
22.104 ms : in_array

Updated 2014-01-02: added noop loop to "zero the scale". Running PHP 5.4.17 on a new MacBook pro, this is a typical result:

2014 年 1 月 2 日更新:添加了 noop 循环以“将比例归零”。在新的 MacBook pro 上运行 PHP 5.4.17,这是一个典型的结果:

24.462 ms : array_search
24.984 ms : in_array

回答by eckes

Based on the documentation of in_arrayand array_search, I'd think that it mainly depends on what you want to do with the information: if you need the entry, use array_search, if you just want to check if the url exists in the array, in_arrayshould be enough.

根据in_arrayarray_search的文档,我认为这主要取决于您想对信息做什么:如果您需要条目,请使用array_search,如果您只想检查 url 是否存在于数组中,in_array应该足够。

回答by AmdY

it's different function in_array - return true if find value array_search - return position if find value

它是不同的函数 in_array - 如果找到值则返回 true array_search - 如果找到值则返回位置

$a = array('a', 'b');
var_dump(in_array('a', $a)); // return true
var_dump(array_search('a', $a)); // return 0 
if (array_search('a', $a)) - false

回答by ekicion

It's up to your array-size. -If you have a small array(like < 500k 32bit-key), in_array and array_search give you same performance isset(array[needle]) make no sense because of flip()

这取决于您的数组大小。- 如果你有一个小数组(比如 < 500k 32bit-key), in_array 和 array_search 给你相同的性能 isset(array[needle]) 因为 flip() 而没有意义

-By big arrays(like > 1m 32bit key) There are really big difference between in_array and isset(array[needle])

- 通过大数组(如 > 1m 32bit 密钥) in_array 和 isset(array[needle]) 之间有很大的区别

回答by Sondre

If you're only goal is to check if an URL exists in the array I'd go for in_array. Altough the best way is having keys set so you can just search by array key. That way you save alot of looping.

如果您的唯一目标是检查数组中是否存在 URL,我会选择 in_array。尽管最好的方法是设置键,这样您就可以按数组键进行搜索。这样你就可以节省大量的循环。

$searchword = "test";
echo $array[$searchword];

回答by Ravish Nirvan

array1=array("a"=>"one","b"=>"two"); 

if(in_array("one",$array))
{
  echo "array exit"; 
 }
 else
  {
     echo " array not exist"; 
  }

echo "</br>";
//example of array_search():
 $b1=array("a"=>"one","b"=>"two");
    echo array_search("one",$b1); 

in_array return true and false value and array_search return key of the array

in_array 返回真假值,array_search 返回数组的键