in my symfony project, I need to be able to merge several arrays of objects while removing duplicates.
For example :
array 1 :
Array
(
[0] => Absence Object
(
[id] => 1
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 12-11-2019 00:00:00
)
[1] => Absence Object
(
[id] => 2
[type] => TypeConge Object ([id] => 5, [nom] => "CA")
[user] => User Object (......)
[debut] => 13-11-2019 00:00:00
)
[2] => Absence Object
(
[id] => 3
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 11-11-2019 00:00:00
)
)
Array 2:
Array
(
[0] => Absence Object
(
[id] => 1
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 12-11-2019 00:00:00
)
[1] => Absence Object
(
[id] => 8
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 17-11-2019 00:00:00
)
)
output:
Array
(
[0] => Absence Object
(
[id] => 1
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 12-11-2019 00:00:00
)
[1] => Absence Object
(
[id] => 2
[type] => TypeConge Object ([id] => 5, [nom] => "CA")
[user] => User Object (......)
[debut] => 13-11-2019 00:00:00
)
[2] => Absence Object
(
[id] => 3
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 11-11-2019 00:00:00
)
[3] => Absence Object
(
[id] => 8
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 17-11-2019 00:00:00
)
)
I use this code to do it :
$demandes = $this->getDemandesValidateur($unUser, $search, $superValidation);
$tabDemandes = array_merge($tabDemandes, $demandes);
$tabDemandes = array_map("unserialize", array_unique(array_map("serialize", $tabDemandes)));
But I get the impression that it causes bugs because of serialization and deserialization. I get the good final table, but some data seems unusable. For example I can not call:
$absence->getType()->getNom();
It returns null sometimes. Is it because of my code ?
My case :
To clarify my case, here's what I'm supposed to do:
I have an empty board at the beginning. Then I make a loop to retrieve other tables that I merge. And in the end, I remove the duplicates.
It looks like this:
case "ROLE_SUPPLEANT":
$tabDemandes = [];
$users = $this->repoUsers->getUsersWhereIsSuppleant($user);
foreach($users as $unUser)
{
$demandes = array_column($this->getDemandesCongesForCalendar($unUser, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
$tabDemandes = $tabDemandes + $demandes;
}
if($user->hasRole("ROLE_VALIDATEUR"))
{
$demandes = array_column($this->getDemandesCongesForCalendar($user, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
$tabDemandes = $tabDemandes + $demandes;
}
break;
Knowing that the function getDemandesCongesForCalendar () returns an array of objects (which themselves contain objects).
I have the impression of having misused the code that you advised me, because it will not remove duplicates I think at the end. The variable $ requests will in any case contain an array with unique values, there will be no duplicates. But since each time, I add its values to the general table ($ tabDemands), there may be duplicates in the latter. And that's where I should step in to remove duplicates