note 24818 added to function.array-unique

From: Date: Thu, 29 Aug 2002 06:39:17 +0000
Subject: note 24818 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-35968@lists.php.net to get a copy of this message
<? $truc = array("l810u00","l810u00","l810q00"); $machin = array_unique($truc); for($i=0;$i < count($machin) ; $i++){ print $machin[$i]." "; } ?> result : l810u00 This is not strange: $machin (as returned by array unique), contains "l810u00" either in key[0] or key[1] but not both (the key depends on the ersion of PHP), and "l810q00" in key[2]. The returned array has TWO elements so count($machin)==2. The returned array has a hole in it, and you're not displaying its full content. You could verify it by using this display loop instead: foreach($machine as $key=>$value){ print '[' . $key . '] => ' . $value . ' "; } result: [0] => l810q00 [2] => l810u00 (the first line may display [1] instead of [0] for PHP 4.0.1p3, but you'll get the same order of values and two lines, as expected). When calling array_values() on the result, you're building a new array with the same values in the same order, but with renumbered keys (without holes in numeric keys). -- http://www.php.net/manual/en/function.array-unique.php http://master.php.net/manage/user-notes.php?action=edit+24818 http://master.php.net/manage/user-notes.php?action=delete+24818 http://master.php.net/manage/user-notes.php?action=reject+24818

« previous php.notes (#35968) next »