Created
June 20, 2017 15:08
-
-
Save calexandrepcjr/15cfcfffab893906869461b3484a3a8d to your computer and use it in GitHub Desktop.
A helper to play with objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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