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 7852767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:30:03+00:00 2026-06-02T19:30:03+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? In the case below, I want to remove elements 0 and 2 because the name is the same. I figured I could loop over every case but that seems wasteful.

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-02T19:30:04+00:00Added an answer on June 2, 2026 at 7:30 pm

    You can loop over the array, calculating how many times each name appears. Then you filter away those elements appearing more than once using array_filter. This will have an average runtime complexity of O(n) (assuming O(1) for arrays), compared to the naive O(n^2) case.

    Code:

    $counter = array();
    foreach($array as $val) {
      $n = $val->name;
      $counter[$n] = (!isset($counter[$n])) ? 1 : 2;
    }
    $result = array_filter(
                $array,
                function ($item) use ($counter) { return $counter[$item->name] == 1; }
              );
    

    For PHP versions before 5.3 you cannot use closures, so in that case, modify your code to this:

    $counter = array();
    foreach($array as $val) {
      $n = $val->name;
      $counter[$n] = (!isset($counter[$n])) ? 1 : 2;
    }
    
    function my_filter($item) {
        global $counter;
        return $counter[$item->name] == 1;
    }
    $result = array_filter(
                $array,
                'my_filter'
              );
    

    Note that this is only useful in reality for large arrays since there’s some overhead involved. It may also be hard to read if you don’t understand what’s being done. For small arrays (a few hundred elements or so) I recommend you to use the naive quadratic-time loop approach.

    • 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
What if instead of removing duplicate elements from an array, I want to remove
I'm using numpy.delete to remove elements from an array that is inside a while
I want to remove all elements from a ContentPanel on a mouse click event
I want to remove specific elements from my List. I don't want to do
I want to remove some elements from a vector and am using remove_if algorithm
In javascript, how do I remove an element from an array of objects? Here
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
I am trying to remove elements from an array if a value in that

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.