note 53260 deleted from function.array-keys by danbrown

From: Date: Wed, 25 Aug 2010 01:50:16 +0000
Subject: note 53260 deleted from function.array-keys by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172014@lists.php.net to get a copy of this message
Note Submitter: xorithNOSPAM (at) alsherok d.o.t net ---- Notes for steve and sip from below (wow, spanning years on these notes!) First off, Steve is right - isset will *not* return true at all when checking for a key that has a value set to the built-in constant null. Sip is right in that it is faster to use isset. However, however his facts seem a bit misleading. Let me show you my console output for my test: [Notice] Populating test array with 200000 values... [Notice] Setting key '100000' to null for null key test. [array_key_exists] Starting timer... [array_key_exists] Found key 150000, time: 0.00015640 [array_key_exists] Test Complete. [isset] Starting timer... [isset] Found key 150000, time: 0.00008583 [isset] Test Complete. [array_key_exists:bad_key] Starting timer... [array_key_exists:bad_key] Did not find bad key 400000, time: 0.00009155 [array_key_exists:bad_key] Test Complete. [isset:bad_key] Starting timer... [isset:bad_key] Did not find bad key 400000, time: 0.00008392 [isset:bad_key] Test Complete. [array_key_exists:null_test] Starting timer... [array_key_exists:null_test] Found key with null value 100000, time: 0.00008392 [array_key_exists:null_test] Test Complete. [isset:null_test] Starting timer... [isset:null_test] Did not find key with null value 100000, time: 0.00008392 [isset:null_test] Test Complete. Yes, that's 200,000 values. I was using an md5 of microtime() and the index, to ensure that the data was of some sort of adequate size. I had to bump up my memory_limit to do the 200,00 index test. One thing I'd like to note is I also tried this test with 2,000 and 20,000. What I found is that the times are almost identical all the way up to 200,000. The time is in seconds. As you can see, while there is a significant time difference between array_key_exists and isset in the first test, the time appeared to improve down the board, until it was equal with isset in the last test. You might also note that really, the time isn't too bad, not for a function that will return a more accurate result than isset. One more test, this time 500,000 indicies: [Notice] Populating test array with 500000 values... [Notice] Setting key '250000' to null for null key test. [array_key_exists] Starting timer... [array_key_exists] Found key 375000, time: 0.00016403 [array_key_exists] Test Complete. [isset] Starting timer... [isset] Found key 375000, time: 0.00008011 [isset] Test Complete. [array_key_exists:bad_key] Starting timer... [array_key_exists:bad_key] Did not find bad key 1000000, time: 0.00007629 [array_key_exists:bad_key] Test Complete. [isset:bad_key] Starting timer... [isset:bad_key] Did not find bad key 1000000, time: 0.00007629 [isset:bad_key] Test Complete. [array_key_exists:null_test] Starting timer... [array_key_exists:null_test] Found key with null value 250000, time: 0.00007439 [array_key_exists:null_test] Test Complete. [isset:null_test] Starting timer... [isset:null_test] Did not find key with null value 250000, time: 0.00007629 [isset:null_test] Test Complete. Surprisingly, the times seem a tad shorter here. This could be a result of my server though, but the fact still stands that even with an incredibly large array, the time impact isn't a huge problem with array_key_exists. So to wrap this up: If you care to know if a key exists, even if it's null, use array_key_exists. If you don't want to know if a key is there if it's null, use isset. -- JWalker (Xorith)

« previous php.notes (#172014) next »