Home remove duplicate arrays in loop
Reply: 0

remove duplicate arrays in loop

user6614
1#
user6614 Published in September 20, 2018, 10:23 pm

I have an associative array that might contain duplicates. I am trying to loop through the array and compare the current element with the next element in the array. If there is a duplicate, it should be removed.

The code below removes one instance of the element. In the test array I'm using, I have 3 duplicate part numbers, but my code only removes one. I'm left with two. I only want one to remain.

  $length = count($items);

   for($i = 0; $i < $length -1; $i++){
    if($items[$i]['part_number'] == $items[$i+1]['part_number']){
      unset($items[$i+1]);
      $items = array_values($items);
    }
   }

What am I doing wrong here?

share|improve this question
  • The element is duplicated or only part_number? – vpalade Feb 13 at 7:41
  • Possible duplicate of Remove duplicates from an array based on object property? – Chrille Feb 13 at 7:42
  • 2
    Can you give the test array? – Fil Feb 13 at 7:42

5 Answers 5

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324226 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO