note 98247 added to function.array-unique

From: Date: Thu, 03 Jun 2010 22:57:45 +0000
Subject: note 98247 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-169870@lists.php.net to get a copy of this message
In case anybody else read the note in the documentation "Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used." and thought that this function would use your __toString() function to do the string comparison, don't be fooled. array_unique() will return your array unchanged. Example: <?php class toStringClass { public $a; public function __construct($a) { $this->a = $a; } public function __toString() { return strval($this->a); } } $input = array(new toStringClass(4), new toStringClass('4'), new toStringClass("3"), new toStringClass(3), new toStringClass('3')); print_r($input); echo "\n"; print_r(array_unique($input)); // outputs same as previous line ?> You will have to write your own comparison function or use one of the ones posted below instead. ---- Server IP: 64.71.164.2 Probable Submitter: 67.171.160.131 ---- Manual Page -- http://www.php.net/manual/en/function.array-unique.php Edit -- https://master.php.net/note/edit/98247 Del: integrated -- https://master.php.net/note/delete/98247/integrated Del: useless -- https://master.php.net/note/delete/98247/useless Del: bad code -- https://master.php.net/note/delete/98247/bad+code Del: spam -- https://master.php.net/note/delete/98247/spam Del: non-english -- https://master.php.net/note/delete/98247/non-english Del: in docs -- https://master.php.net/note/delete/98247/in+docs Del: other reasons-- https://master.php.net/note/delete/98247 Reject -- https://master.php.net/note/reject/98247 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169870) next »