How can I compare objects inside of an array?
I have a dynamically populated array of simpleXML objects (length is unknown)
Is there a fast way to compare all those objects inside the array and exclude duplicates?
And its getting a bit harder because ID field is different in every simpleXML object so I need to omit it in comparison.
For example simple short version:
Array
(
[0] => SimpleXMLElement Object
(
[ID] => 556
[FIRST] => JOHN
[MID] => SimpleXMLElement Object
(
)
[LAST] => SMITH
)
[1] => SimpleXMLElement Object
(
[ID] => 557
[FIRST] => JOHN
[MID] => SimpleXMLElement Object
(
)
[LAST] => SMITH
)
)
These are 2 duplicates. I need to confirm it and remove one.
This is what i have so far:
$noDups = array();
foreach($arr as $key=>$value)
{
if (0 == $key) {
$noDups[] = $value;
continue;
}
foreach($arr as $_key=>$_value)
{
$first = ( ((string)$value->FIRST) !== ((string)$_value->FIRST) )?true:false;
$mid = ( ((string)$value->MID) !== ((string)$_value->MID) )?true:false;
$last = ( ((string)$value->LAST) !== ((string)$_value->LAST) )?true:false;
if ($first && $mid && $last)
{
array_push($noDups, $value);
}
}
}
But i don't find this code sexy if you know what i mean.