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