2

Consider following snippets of code:

Example #1

$array = Array(1,2,3,4,5,6,7);
$array_test = Array(3,5,4,7,3,6,7,8,8,9,3);

foreach($array_test as $value) {
   if(in_array($value, $array)) {
       // do some magic here
   }
}

Example #2

$array = Array(1,2,3,4,5,6,7);
$array_test = Array(3,5,4,7,3,6,7,8,8,9,3);

$array_index = Array();
foreach($array as $value) {
    $array_index[ $value ] = true;
}

foreach($array_test as $value) {
   if(isset($array_index[ $value ])) {
       // do some magic here
   }
}

Obviously both snippets do the same jobs. In some array samples example #1 is faster than example #2.

I am sure we all were in both situations, however my question is:

  • Should I always choose #2?
  • When should I choose #1? When size of $array * $array_test is < 10? <100? <1000?
  • How to determine which method is better in particular situation?
  • Maybe there is some other trick than using temp table $array_index. I don't remember similar situation in other programming languages, everything was ready as-you-go

Please mind about associative keys too.

Someone already asked very similar question:

Cœur
  • 37,241
  • 25
  • 195
  • 267
Peter
  • 16,453
  • 8
  • 51
  • 77
  • 1
    `in_array` has to use a linear time search to find your value. Looking for a key, though, is a constant time operation since PHP arrays are hash tables. So `in_array` should be the slowest. (If you have to write all your elements to a temporary array each time, though, it might not exactly be any faster.) – zneak Jan 23 '13 at 02:55
  • 2
    maybe try [`array_intersect`](http://php.net/manual/en/function.array-intersect.php)? – Fabrício Matté Jan 23 '13 at 03:00
  • array_diff and array_interesct doesn't fit here, consider common siutation where `$array_test` is just array of rows from db – Peter Jan 23 '13 at 03:03
  • I see. `array_intersect` would most likely be slower as well. `isset` should be much faster as @zneak commented. – Fabrício Matté Jan 23 '13 at 03:05

2 Answers2

4

In your second example, you have to construct the "flipped" value of $array before you can use isset(). Btw, you can also use array_flip() for that.

If you can use array keys immediately (without conversion), use isset() because it's obviously much faster than in_array() due to way keys are looked up and because it's a language construct.

If you can't use the keys without conversion, you could consider using in_array() for small arrays. For bigger arrays it might be worthwhile to run a benchmark to determine whether an array conversion step would still be worth it.

Lastly, and depending largely on the situation, you could use one of the array_intersect_ functions as well, mainly to avoid having to loop inside PHP code.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • i'll do some benchmarks tomorrow. `array_intersect` doesn't fit here as the most common situation is (for example) when I compare array of emails to array of rows from database – Peter Jan 23 '13 at 03:13
  • @PeterSzymkowski I think that should be mentioned in your question as well :) – Ja͢ck Jan 23 '13 at 04:04
1

isset will always be faster than in_array because keys are unique and values are not. The fact that keys are unique give optimization possibilities that are impossible with values.