title: Why array_unique does not detect duplicate objects?
body: <p>I can't seem to figure out what magic is happening behind the PHP scene and why <a href="https://www.php.net/manual/en/function.array-unique.php" rel="noreferrer">array_unique</a> cannot detect my duplicates.</p>
  
  <p>In my specific situation, I have 2 collections of users, which I am merging into one and then keeping only unique entries. For that I am converting both collections into arrays, <code>array_merge()</code> them and then based on parameter apply <code>array_unique(..., SORT_REGULAR)</code> so that they are compared as objects without any conversions. I realise that comparing objects is a slippery slope, but in this case it's weirder than I though.</p>
  
  <p>After merge but before the uniqueness check I have this state:
  <a href="https://i.stack.imgur.com/SsKQO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SsKQO.png" alt="enter image description here"></a></p>
  
  <p>As you can see, items 4 and 11 are the same User entity (both non-strict and strict comparison agree on that). Yet after <code>array_unique()</code> they both remain in the list for some reason:
  <a href="https://i.stack.imgur.com/TCBfm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TCBfm.png" alt="enter image description here"></a></p>
  
  <p>As you can see, items 7-10 were detected and removed, but 11 wasn't.</p>
  
  <p>How is that possible? What am I not seeing here?</p>
  
  <p>Currently running PHP 7.4.5</p>
  
  <p>Code is from project using Symfony 4.4.7 and Doctrine ORM 2.7.2 (although I think this should be irrelevant, if the objects are equal both by <code>==</code> and <code>===</code> comparisons).</p>
  
  <p>Fun fact for bonus points - applying <code>array_unique</code> twice in a row gives actually unique results:
  <a href="https://i.stack.imgur.com/ZB3ph.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZB3ph.png" alt="enter image description here"></a></p>
  
  <p>Mind = blown</p>
  
  <p><strong>UPDATE:</strong> I have added <code>throw new \RuntimeException()</code> in my <code>User::__toString()</code> method, to be extra sure noone is doing conversion to string.</p>
  
  <p>Please do not suggest converting to string - that is neither a solution to my problem, nor what this question is about.</p>