Hi! Is there any method like the array_unique for objects? I have a bunch of arrays with 'Role' objects that I merge, and then I want to take out the duplicates :)
Thanks
Hi! Is there any method like the array_unique for objects? I have a bunch of arrays with 'Role' objects that I merge, and then I want to take out the duplicates :)
Thanks
Well array_unique()
compares the string value of the elements:
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same. The first element will be used.
So make sure to implement the __toString()
method in your class and that it outputs the same value for equal roles, e.g.
class Role {
private $name;
//.....
public function __toString() {
return $this->name
}
}
This would consider two roles as equal if they have the same name.