Hi internals,
I know HEAD is freezed, but anyway to ask
whether a new function in_array_all() can be added.
It works like in_array()
but checks whether all needles
are in the stack array.
Diff : http://www.hristov.com/andrey/projects/php_stuff/in_array_all.diff.txt
Best wishes,
Andrey
I know HEAD is freezed, but anyway to ask whether a new function
in_array_all() can be added. It works likein_array()
but checks
whether all needles are in the stack array.
Do we really need this either way?
function in_array_all($needles, $haystack, $strict = false) {
foreach($needles as $needle) {
if (!in_array($needle, $haystack, $strict)) {
return false;
}
}
return true;
}
-adam
--
adam@trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!
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
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
Hi,
just a correction, I meant array_diff when i wrote array_intersect.
array_diff is more handy since just count($res)==0 is enough, $res is the
returned array of array_diff()
.
Andrey
Andrey Hristov wrote:
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.03888Andrey
Hi,
just a correction, I meant array_diff when i wrote array_intersect.
array_diff is more handy since just count($res)==0 is enough, $res is the
returned array ofarray_diff()
.
Did you actually try array_intersect()
?
- Andrei
Hi Andrei :)
Andrei Zmievski wrote:
Hi,
just a correction, I meant array_diff when i wrote array_intersect.
array_diff is more handy since just count($res)==0 is enough, $res is the
returned array ofarray_diff()
.Did you actually try
array_intersect()
?
- Andrei
Here it is :
andrey@poohie:~/dev/5_0> cat s2.php
<?php
$i = 0;
while ($i++ < 1000) {
$a[] = $i;
}
$start = microtime(1);
$res = in_array_all(100, 2, 3, $a, true);
$end = microtime(1);
var_dump($res);
printf("Time : %3.5f\n",$end - $start);
$needles = array(100, 2, 3);
$start = microtime(1);
$diff = array_intersect($needles, $a);
$end = microtime(1);
var_dump($diff);
printf("Time : %3.5f\n",$end - $start);
?>andrey@poohie:~/dev/5_0> ./php s2.php
bool(true)
Time : 0.00010
array(3) {
[0]=>
int(100)
[1]=>
int(2)
[2]=>
int(3)
}
Time : 0.03078
About 300x .
Andrey
Hi Andrey,
My personal feeling is that not enough people use this to make the pure
speed improvement worth a new function. I guess the list will decide...
- Chris
Hi,
I didn't write this function to be faster. You asked about array_intersect()
and I decided to bechmark (you didn't ask for it though). What I had in mind
when I decided to write it was to escape this :
if (in_array("some_element", $haystack) && in_array("other_element", $haystack) &&
in_array("third_element", $haystack))
which with this function will be :
in_array_all("first_element","second_element", "third_element", $haystack)
In addtion, I have no problem with the proposal the needles to be passed as an array as
first parameter. I did the current proto in the way it's now since it's more consistent
with in_array()
.
Finally, I hope that the list will decide.
Andrey
Christian Schneider wrote:
Hi Andrey,
My personal feeling is that not enough people use this to make the pure
speed improvement worth a new function. I guess the list will decide...
- Chris
Andrey Hristov wrote:
Hi,
I didn't write this function to be faster. You asked about
array_intersect()
and I decided to bechmark (you didn't ask for it though). What I had
in mind
when I decided to write it was to escape this :
if (in_array("some_element", $haystack) && in_array("other_element",
$haystack) && in_array("third_element", $haystack))which with this function will be :
in_array_all("first_element","second_element", "third_element",
$haystack)In addtion, I have no problem with the proposal the needles to be
passed as an array as first parameter. I did the current proto in the
way it's now since it's more consistent within_array()
.Finally, I hope that the list will decide.
Andrey
Christian Schneider wrote:
Hi Andrey,
My personal feeling is that not enough people use this to make the
pure speed improvement worth a new function. I guess the list will
decide...
- Chris
I like the idea of passing an array of needles, I wonder if you could
just add the ability to pass a needles array to in_array, rather than
create a new function.
Anyways, it adds flexability, I would use it.
Jason
Hi,
jason davidson wrote:
I like the idea of passing an array of needles, I wonder if you could
just add the ability to pass a needles array to in_array, rather than
create a new function.
Anyways, it adds flexability, I would use it.Jason
Unfortunately, in_array()
cannot be modified since this will break BC.
Consider this example :
php -r '$a = array(1, 2); $haystack = array("string", array(1, 2), 5.5);
var_dump(in_array($a, $haystack));'
So, in_array()
checks for everything, including arrays, starting PHP5 it can look for
objects too. So the BC is that till now an array as first parameter is quite ok therefore
it cannot be changed.
Andrey
Andrey Hristov wrote:
Hi,
jason davidson wrote:
I like the idea of passing an array of needles, I wonder if you could
just add the ability to pass a needles array to in_array, rather than
create a new function.
Anyways, it adds flexability, I would use it.Jason
Unfortunately,
in_array()
cannot be modified since this will break BC.
Consider this example :
php -r '$a = array(1, 2); $haystack = array("string", array(1, 2),
5.5); var_dump(in_array($a, $haystack));'So,
in_array()
checks for everything, including arrays, starting PHP5
it can look for objects too. So the BC is that till now an array as
first parameter is quite ok therefore
it cannot be changed.Andrey
Sorry, i hadnt even realized that you could already pass needles array
into in_array, and now i beleive ive missed the whole point of this
thread.. Im done.. :)
J
Andrey Hristov wrote:
Hi internals,
I know HEAD is freezed, but anyway to ask
whether a new function in_array_all() can be added.
It works likein_array()
but checks whether all needles
are in the stack array.Diff :
http://www.hristov.com/andrey/projects/php_stuff/in_array_all.diff.txtBest wishes,
Andrey
Does this mean that one of the arguments could/should be an array?
if(in_array($needles_array, $haystack_array))
{ /*err.... */ }
Cheers,
BDKR
Hi internals,
I know HEAD is freezed, but anyway to ask
whether a new function in_array_all() can be added.
It works likein_array()
but checks whether all needles
are in the stack array.Diff :
http://www.hristov.com/andrey/projects/php_stuff/in_array_all.diff.txt
Maybe just use array_intersect()
instead?
- Andrei