1

Is there a difference in O(n) of the below operations?

$a1 = [1=>'',2=>'', 3=>'']
isset($a1[2])

$a2 = [1,2, 3]
in_array(2, $a2)
ericsicons
  • 1,475
  • 3
  • 23
  • 38
  • 1
    [in_array](http://php.net/manual/en/function.in-array.php) for values... sooo... they're not really meant for the same thing. – gloomy.penguin Oct 25 '14 at 02:01
  • 1
    this is my guess... `isset` is faster because `in_array()` must look at the values. `isset` just wants to know if that index exists regardless of what it actually holds. – gloomy.penguin Oct 25 '14 at 02:02
  • 2
    possible duplicate of [what is faster: in\_array or isset?](http://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset) – rjdown Oct 25 '14 at 02:34

1 Answers1

2

isset($a1[2]) has complexity of O(1)
in_array(2, $a2) has complexity of O(3) in your case or in general O(N) where N=count_of_the_array elements

Antoan Milkov
  • 2,152
  • 17
  • 30