title: PHP - Merge two arrays of objects included other objects
body: <p>in my symfony project, I need to be able to merge several arrays of objects while removing duplicates.</p>
  
  <p>For example :</p>
  
  <p><strong>array 1 :</strong></p>
  
  <pre><code>Array
  (
      [0] =&gt; Absence Object
          (
              [id] =&gt; 1
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 12-11-2019 00:00:00
          )
  
      [1] =&gt; Absence Object
          (
              [id] =&gt; 2
              [type] =&gt; TypeConge Object ([id] =&gt; 5, [nom] =&gt; "CA")
              [user] =&gt; User Object (......)
              [debut] =&gt; 13-11-2019 00:00:00
          )
  
      [2] =&gt; Absence Object
          (
              [id] =&gt; 3
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 11-11-2019 00:00:00
          )    
  )
  </code></pre>
  
  <p><strong>Array 2:</strong></p>
  
  <pre><code>Array
  (
      [0] =&gt; Absence Object
          (
              [id] =&gt; 1
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 12-11-2019 00:00:00
          )
  
      [1] =&gt; Absence Object
          (
              [id] =&gt; 8
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 17-11-2019 00:00:00
          )    
  )
  </code></pre>
  
  <p><strong>output:</strong></p>
  
  <pre><code>    Array
  (
      [0] =&gt; Absence Object
          (
              [id] =&gt; 1
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 12-11-2019 00:00:00
          )
  
      [1] =&gt; Absence Object
          (
              [id] =&gt; 2
              [type] =&gt; TypeConge Object ([id] =&gt; 5, [nom] =&gt; "CA")
              [user] =&gt; User Object (......)
              [debut] =&gt; 13-11-2019 00:00:00
          )
  
      [2] =&gt; Absence Object
          (
              [id] =&gt; 3
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 11-11-2019 00:00:00
          )    
      [3] =&gt; Absence Object
          (
              [id] =&gt; 8
              [type] =&gt; TypeConge Object ([id] =&gt; 4, [nom] =&gt; "Maladie")
              [user] =&gt; User Object (......)
              [debut] =&gt; 17-11-2019 00:00:00
          )    
  
  )
  </code></pre>
  
  <p>I use this code to do it :</p>
  
  <pre><code>$demandes = $this-&gt;getDemandesValidateur($unUser, $search, $superValidation);
  $tabDemandes = array_merge($tabDemandes, $demandes);
  $tabDemandes = array_map("unserialize", array_unique(array_map("serialize", $tabDemandes)));
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$absence-&gt;getType()-&gt;getNom();
  </code></pre>
  
  <p>It returns null sometimes. Is it because of my code ?</p>
  
  <p>My case :</p>
  
  <p>To clarify my case, here's what I'm supposed to do:</p>
  
  <p>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.</p>
  
  <p>It looks like this:</p>
  
  <pre><code>                case "ROLE_SUPPLEANT":
                      $tabDemandes = [];
                      $users = $this-&gt;repoUsers-&gt;getUsersWhereIsSuppleant($user);
  
                      foreach($users as $unUser)
                      {
                          $demandes = array_column($this-&gt;getDemandesCongesForCalendar($unUser, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
                          $tabDemandes = $tabDemandes + $demandes;
                      }
                      if($user-&gt;hasRole("ROLE_VALIDATEUR"))
                      {
                          $demandes = array_column($this-&gt;getDemandesCongesForCalendar($user, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
                          $tabDemandes = $tabDemandes + $demandes;
                      }
                      break;
  </code></pre>
  
  <p>Knowing that the function getDemandesCongesForCalendar () returns an array of objects (which themselves contain objects).</p>
  
  <p>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</p>