note 75351 added to function.is-array

From: Date: Fri, 25 May 2007 06:25:35 +0000
Subject: note 75351 added to function.is-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-126884@lists.php.net to get a copy of this message
First, "sequential array" is a silly term. Use "vector." Likewise, it's not an "associative array," it's a "map." We have common computer science terms for a reason. :) "If the keys were all numbers then we'd call it "sequential"." But that's not true. The following array, with all-numeric keys, is a map, not a vector: $ar[1] = 'foo'; $ar[12] = 'bar'; $ar[578] = 'baz'; A vector would be an array in which all keys are integers, each successive key is equal to the previous key plus one, and the first key is 0. So, the only proper, always correct, fail-proof way I can think of to do this is (and this is lame we have to do ths, since the internal PHP implementation clearly knows in a much more efficient manner): function is_vector (&$array) { $next = 0; foreach ($array as $k=>$v) { if ($k != $next) return false; $next++; } return true; } Yes, it might look cleaner to use array_keys() in there, but I think that will be far more inefficient, since it would have to create a whole new array and copy all the keys into it. The above should be faster. In theory. ---- Server IP: 69.147.83.197 Probable Submitter: 71.238.87.27 ---- Manual Page -- http://www.php.net/manual/en/function.is-array.php Edit -- https://master.php.net/note/edit/75351 Del: integrated -- https://master.php.net/note/delete/75351/integrated Del: useless -- https://master.php.net/note/delete/75351/useless Del: bad code -- https://master.php.net/note/delete/75351/bad+code Del: spam -- https://master.php.net/note/delete/75351/spam Del: non-english -- https://master.php.net/note/delete/75351/non-english Del: in docs -- https://master.php.net/note/delete/75351/in+docs Del: other reasons-- https://master.php.net/note/delete/75351 Reject -- https://master.php.net/note/reject/75351 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126884) next »