note 113870 deleted from function.array-unique by cmb
| From: | cmb@php.net | Date: | Mon, 12 Nov 2018 14:36:34 +0000 |
| Subject: | note 113870 deleted from function.array-unique by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209674@lists.php.net to get a copy of this message | ||
Note Submitter: Felipe CWB
----
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);
?>