How do I remove elements from an array of objects with the same name so it exists only once?
In the case below, I want to remove element 2 because the name is the same as element 0.
(It doesn’t matter to me that the term id or count is different).
array(1) {
[0]=> object(stdClass)#268 (3) {
["term_id"]=> string(3) "486"
["name"]=> string(4) "2012"
["count"]=> string(2) "40"
}
[1]=> object(stdClass)#271 (3) {
["term_id"]=> string(3) "488"
["name"]=> string(8) "One more"
["count"]=> string(2) "20"
}
[2]=> object(stdClass)#275 (3) {
["term_id"]=> string(3) "512"
["name"]=> string(8) "2012"
["count"]=> string(2) "50"
}
If object’s ‘name’ property is public, like this:
$objArrayis the original Array, and$filteredObjArris what you need.If ‘name’ property is not public, use method to return ‘name’ instead of
$obj->name. For example:If the class has no method to return ‘name’, you have to add it:
If you don’t wanna change original class, extend it: