title: PHP array_diff with instances or custom callback function?
body: <p>I have many arrays, each holding instances of <code>Product</code>. Need to get unique products only. From PHP documentation of <a href="http://www.php.net/manual/en/function.array-diff.php" rel="nofollow"><code>array_diff</code></a>:</p>
  
  <pre><code>array array_diff ( array $array1 , array $array2 [, array $... ] )
  </code></pre>
  
  <blockquote>
    <p>Two elements are considered equal if and only if (string) $elem1 ===
    (string) $elem2. In words: when the string representation is the same.</p>
  </blockquote>
  
  <p>Does this mean I'm forced to implement <code>toString()</code> in my instances? Is there any function to compute the difference providing a custom callback?</p>
  
  <p>I didn't tested this code, but I guess it will not work because there is no <code>toString()</code> function in <code>Product</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>$categories = array();
  // ...
  
  // Unique products from all categories, compared against ===
  $uniqueProducts = array();
  
  // Compute unique products
  foreach($categories as $category) {
      $uniqueProducts += array_diff($category-&gt;getProducts(), $uniqueProducts)
  }
  
  return $uniqueProducts;
  </code></pre>