Hi Christian,
Christian Schneider wrote:
> Andrey Hristov wrote:
>
>> It works like in_array() but checks whether all needles
>> are in the stack array.
>
>
> Looking at the prototype:
> bool in_array_all(mixed needle1[, mixed needle2], mixed haystack [, bool
> strict])
>
> I'd say if (and I'm personally -1) this is added it should be
>
> bool in_array_all(array needles, array haystack [, bool strict])
>
> IMHO which would make parameter handling much simpler.
>
> But then again you could also do array_intersect($needles, $haystack) to
> get the same result in a lot of cases.
>
> - Chris
>
So, I did now some testing. Looks like in_array_all() is 6x faster than array_intersect()
the haystack is 10 elements, and needles are 3.
When 1000 elements in haystack, the fastest time (when the needles are in the beggining
in_array_all is 300x faster. When the whole haystack should be traversed it's about 250x
faster than array_intersect. When one of the needles is not in the haystack : 300x .
Script :
<?php
$i = 0;
while ($i++ < 1000) {
$a[] = $i;
}
$start = microtime(1);
$res = in_array_all(100, 2, 3, $a, true);
$end = microtime(1);
printf("Time : %3.5f\n",$end - $start);
$needles = array(100, 2, 3);
$start = microtime(1);
$diff = array_diff($needles, $a);
$end = microtime(1);
var_dump($diff);
printf("Time : %3.5f\n",$end - $start);
?>
andrey@poohie:~/dev/5_0> ./php s.php
bool(true)
Time : 0.00010
array(0) {
}
Time : 0.03888
Andrey