I often come across a scenario where I have two collections of objects (either array or IteratorAggregate class) and need to diff the two lists.
By diff, I mean:
- Detect duplicate objects (logic for detecting duplicates would vary case-by-case)
- Add new objects
- Remove objects that aren’t in the other list
Essentially, I’m looking for something like array_diff that works with objects. Up to now, I’ve just been writing the same logic over and over for each type of collection. Obviously, since the conditions for duplicate objects will differ from case to case, there’s not a singular solution. But is there a common pattern or abstraction that people have found to be an elegant way to deal with this?
spl_object_hashwill help you determine if two objects are the same.