Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:48:35+00:00 2026-06-02T20:48:35+00:00

How do I remove elements from an array of objects with the same name

  • 0

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"  
 } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:48:36+00:00Added an answer on June 2, 2026 at 8:48 pm

    If object’s ‘name’ property is public, like this:

    $filteredObjArr = array();
    $existNames = array();
    foreach($objArray as $k => $obj) {
      if(!in_array($obj->name, $existNames)) {
        $filteredObjArr[$k] = $obj;
        $existNames[] = $obj->name;
      }
    }
    

    $objArray is the original Array, and$filteredObjArr is what you need.

    If ‘name’ property is not public, use method to return ‘name’ instead of $obj->name. For example:

    $obj->getName(); //this method name is example
    

    If the class has no method to return ‘name’, you have to add it:

    Class OriginalClass {
    
        //...something
    
        // add
        public function getName() {
            return $this->name;
        }
    }
    

    If you don’t wanna change original class, extend it:

    Class ExtendedClass extends OriginalClass {
        public function getName() {
            return $this->name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I remove elements from an array of objects with the same name?
In javascript, how do I remove an element from an array of objects? Here
What if instead of removing duplicate elements from an array, I want to remove
How do I remove an element from an array when I know the element's
I want to remove some elements from a vector and am using remove_if algorithm
Possible Duplicates: How do I remove objects from an Array in java? Removing an
I want to remove an item from an array, is array.splice(x,1) the best way
A have an array of Objects and I'd like to remove the first element
Is there a PHP function to remove certain array elements from an array? E.g.,
Possible Duplicate: Remove elements from array? how would I remove all array elements but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.