note 73653 added to function.is-array

From: Date: Mon, 05 Mar 2007 23:22:48 +0000
Subject: note 73653 added to function.is-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-124127@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 194.145.210.3 Probable Submitter: 82.36.245.7 ---- Manual Page -- http://www.php.net/manual/en/function.is-array.php Edit -- https://master.php.net/note/edit/73653 Del: integrated -- https://master.php.net/note/delete/73653/integrated Del: useless -- https://master.php.net/note/delete/73653/useless Del: bad code -- https://master.php.net/note/delete/73653/bad+code Del: spam -- https://master.php.net/note/delete/73653/spam Del: non-english -- https://master.php.net/note/delete/73653/non-english Del: in docs -- https://master.php.net/note/delete/73653/in+docs Del: other reasons-- https://master.php.net/note/delete/73653 Reject -- https://master.php.net/note/reject/73653 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124127) next »