Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Created June 20, 2017 15:08
Show Gist options
  • Save calexandrepcjr/15cfcfffab893906869461b3484a3a8d to your computer and use it in GitHub Desktop.
Save calexandrepcjr/15cfcfffab893906869461b3484a3a8d to your computer and use it in GitHub Desktop.
A helper to play with objects
<?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');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment