note 76055 added to function.is-array

From: Date: Thu, 28 Jun 2007 07:49:46 +0000
Subject: note 76055 added to function.is-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-128044@lists.php.net to get a copy of this message
@elanthis at awesomeplay dot com, your function to check whether an array is a map or a vector has a little flaw. Comparing $k != $value will return true if $value is 0. If you check a map containing just one element like array('foo'=>'bar') your check assumes that this is a vector. Use the operator !== (not identical with) to avoid that: <?php function is_vector (&$array) { $next = 0; foreach ($array as $k=>$v) { if ($k !== $next) return false; $next++; } return true; } ?> ---- Server IP: 217.13.201.10 Probable Submitter: 134.102.230.241 ---- Manual Page -- http://www.php.net/manual/en/function.is-array.php Edit -- https://master.php.net/note/edit/76055 Del: integrated -- https://master.php.net/note/delete/76055/integrated Del: useless -- https://master.php.net/note/delete/76055/useless Del: bad code -- https://master.php.net/note/delete/76055/bad+code Del: spam -- https://master.php.net/note/delete/76055/spam Del: non-english -- https://master.php.net/note/delete/76055/non-english Del: in docs -- https://master.php.net/note/delete/76055/in+docs Del: other reasons-- https://master.php.net/note/delete/76055 Reject -- https://master.php.net/note/reject/76055 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128044) next »