note 113870 added to function.array-unique
| From: | FelipeCWB | Date: | Wed, 11 Dec 2013 16:48:38 +0000 |
| Subject: | note 113870 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197907@lists.php.net to get a copy of this message | ||
array_unique only compares with the string,
But to compare with various types of variables I used like this:
<?php
/**
* master ArrayUnique
* now compare all types
* @param array $array
* @return array
*/
function arrayUnique($array)
{
foreach ($array as $k => $v) {
foreach ($array as $k2 => $v2) {
if (($v2 == $v) && ($k != $k2)) {
unset($array[$k]);
}
}
}
return $array;
}
$a = [];
$a[] = (object)['nome' => 'Bob'];
$a[] = (object)['nome' => 'Peter'];
$a[] = (object)['nome' => 'Jhon'];
$a[] = (object)['nome' => 'James'];
$a[] = (object)['nome' => 'David'];
$a[] = (object)['nome' => 'Bob']; // repeated
if ($a[0] == $a[5]) { // true
echo 'yes';
} else {
echo 'not';
}
echo "\n\n";
$a = arrayUnique($a);
print_r($a);
?>
----
Server IP: 72.55.140.6
Probable Submitter: 201.22.7.154
----
Manual Page -- http://php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/113870
Del: integrated -- https://master.php.net/note/delete/113870/integrated
Del: useless -- https://master.php.net/note/delete/113870/useless
Del: bad code -- https://master.php.net/note/delete/113870/bad+code
Del: spam -- https://master.php.net/note/delete/113870/spam
Del: non-english -- https://master.php.net/note/delete/113870/non-english
Del: in docs -- https://master.php.net/note/delete/113870/in+docs
Del: other reasons-- https://master.php.net/note/delete/113870
Reject -- https://master.php.net/note/reject/113870
Search -- https://master.php.net/manage/user-notes.php