哪个是最好的array_search或in_array?

时间:2022-09-12 22:31:14

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

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

6 个解决方案

#1


30  

Based on the documentation of in_array and 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_array should be enough.

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

#2


50  

If it's a large array and in 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()检查是否存在。

#3


49  

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-01-02:添加noop循环到“零级”。在新的MacBook pro上运行PHP 5.4.17,这是一个典型的结果:

24.462 ms : array_search
24.984 ms : in_array

#4


4  

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

它是不同的函数in_array——查找值返回true——查找值返回位置

$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

#5


0  

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];

#6


0  

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 32位键),in_array和isset(array[needle])之间的差异非常大

#1


30  

Based on the documentation of in_array and 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_array should be enough.

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

#2


50  

If it's a large array and in 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()检查是否存在。

#3


49  

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-01-02:添加noop循环到“零级”。在新的MacBook pro上运行PHP 5.4.17,这是一个典型的结果:

24.462 ms : array_search
24.984 ms : in_array

#4


4  

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

它是不同的函数in_array——查找值返回true——查找值返回位置

$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

#5


0  

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];

#6


0  

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 32位键),in_array和isset(array[needle])之间的差异非常大