note 84488 added to function.is-array

From: Date: Wed, 16 Jul 2008 15:05:41 +0000
Subject: note 84488 added to function.is-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-141567@lists.php.net to get a copy of this message
Yet another simpler, faster is_assoc(): <?php function is_assoc($array) { foreach (array_keys($array) as $k => $v) { if ($k !== $v) return true; } return false; } ?> In my tests it runs about twice as fast as Michael/Gabriel's array_reduce() method. (Speaking of which: Gabriel's version doesn't work as written; it reports associative arrays as numeric if only the first key is non-numeric, or if the keys are numeric but ordered backwards. Michael solves this problem by comparing array_reduce() to count(), but that costs another function call; it also works to just compare to -1 instead of 0, and therefore return -1 as the ternary else from the callback). ---- Server IP: 208.69.120.35 Probable Submitter: 72.55.202.68 ---- Manual Page -- http://www.php.net/manual/en/function.is-array.php Edit -- https://master.php.net/note/edit/84488 Del: integrated -- https://master.php.net/note/delete/84488/integrated Del: useless -- https://master.php.net/note/delete/84488/useless Del: bad code -- https://master.php.net/note/delete/84488/bad+code Del: spam -- https://master.php.net/note/delete/84488/spam Del: non-english -- https://master.php.net/note/delete/84488/non-english Del: in docs -- https://master.php.net/note/delete/84488/in+docs Del: other reasons-- https://master.php.net/note/delete/84488 Reject -- https://master.php.net/note/reject/84488 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141567) next »