Voting

: zero plus nine?
(Example: nine)

The Note You're Voting On

calexandrepcjr at gmail dot com
7 years ago
Following the Ghanshyam Katriya idea, but with an array of objects, where the $key is related to object propriety that you want to filter the uniqueness of array:

<?php
function obj_multi_unique($obj, $key = false)
{
$totalObjs = count($obj);
if (
is_array($obj) && $totalObjs > 0 && is_object($obj[0]) && ($key && !is_numeric($key))) {
for (
$i = 0; $i < $totalObjs; $i++) {
if (isset(
$obj[$i])) {
for (
$j = $i + 1; $j < $totalObjs; $j++) {
if (isset(
$obj[$j]) && $obj[$i]->{$key} === $obj[$j]->{$key}) {
unset(
$obj[$j]);
}
}
}
}
return
array_values($obj);
} else {
throw new
Exception('Invalid argument or your array of objects is empty');
}
}
?>

<< Back to user notes page

To Top