简体   繁体   中英

PHP array performance checking key existence vs checking values existence

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM