note 73653 deleted from function.is-array by danbrown

From: Date: Sat, 27 Dec 2008 16:48:21 +0000
Subject: note 73653 deleted from function.is-array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148562@lists.php.net to get a copy of this message
Note Submitter: adam at greatbigmassive dot net ---- Just looked at all the checks to see if anyone had solved the problem of knowing if an array is "associative" or not. As mentioned, all arrays are "associative" in a sense as keys are automatically set as numbers if not manually done. It's just better to say "associative" so we know that keys are alpha based. If the keys were all numbers then we'd call it "sequential". Here's the common way to create "associative" and "sequential" arrays. Method 1 (Sequential) --------------------- $my_array = Array("january","february"); Method 2 (Associative) ---------------------- $my_array = Array("jan"=>"January","feb"=>"February"); So to me the obvious way to check if it's an associative array or not is to see if the first key of the array is numeric or not. If it's numeric then I know it's sequential, otherwise if it's blank or alphanumberic, i know its associative. I certainly know I wouldnt mix keys so that some were just a number and other were text.... so this check works perfectly for me.. Might be useful to someone else too..... <?php //Get first array line, check key function is_associative($array){ if(is_array($array) && !is_numeric(array_shift(array_keys($array)))){ return true; } return false; } ?>

« previous php.notes (#148562) next »