I'm trying to get to one array that don't have duplicate values for a specific field in an array of objects. I've merged two array's of objects together and am using $unique = array_unique($merged, SORT_REGULAR)
to remove any duplicate values that the merged array might have.
Unfortunately I cannot get a result of one array without the duplicate value (let's say for the email
field) as the resulting array still contains two instances of one email.
It's my understanding that using the SORT_REGULAR
flag is supposed to catch that but it obviously doesn't. Can someone explain why, and perhaps what I'm missing to do it right?
The two objects:
$tracks = $em->getRepository('AppBundle:Trak')->findByGrace($arrayNum);
$grades = $em->getRepository('AppBundle:Grade')->findByGrace($arrayGrad);
How I merge them:
$merge = array_merge($tracks, $grades);
Using Array Unique:
$unique = array_unique($merge, SORT_REGULAR);
My output for $tracks, $merge & $unique
are as follows. They each start exactly the same way (as it uses the first object from $tracks) and the culprit that has the duplicate email is the first instance in the object. I hope this helps:
Array
(
[0] => AppBundle\Entity\Trak Object
(
[id:AppBundle\Entity\Trak:private] => 657
[active:AppBundle\Entity\Trak:private] => 1
[sid:AppBundle\Entity\Trak:private] => 1554
[firstName:AppBundle\Entity\Trak:private] => X
[lastName:AppBundle\Entity\Trak:private] => Y
[title:AppBundle\Entity\Trak:private] => ISW
[organization:AppBundle\Entity\Trak:private] => Self Employed
[email:AppBundle\Entity\Trak:private] => XY@gmail.com
..........