note 98247 deleted from function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Thu, 03 Jun 2010 23:04:04 +0000 |
| Subject: | note 98247 deleted from function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169871@lists.php.net to get a copy of this message | ||
Note Submitter: jojonaloha at yahoo dot com
----
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.