0

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?

3
  • The element is duplicated or only part_number? Commented Feb 13, 2018 at 7:41
  • Possible duplicate of Remove duplicates from an array based on object property? Commented Feb 13, 2018 at 7:42
  • 2
    Can you give the test array? Commented Feb 13, 2018 at 7:42

5 Answers 5

2

You need to loop backwards through the array, and delete the current item.

$length = count($items);

for($i = $length - 1; $i > 0; $i--){
    if($items[$i]['part_number'] == $items[$i-1]['part_number']){
        unset($items[$i]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

That almost works. It seems to be removing too many elements. I have a multidimensional array with 9 records. Each 012 are duplicates, 345 are duplicates and 678 are duplicates. I only want 0,3 and 6. In your example, 6 is removed.
Strange. My code is acting differently. I'll double check my array. Thanks for the help!
I forgot to reindex the array. $items = array_values($items); Working now.
0

becuase your code is The $ items value is in the for statement.

if you want unique array, you have to array_unique function

http://php.net/manual/en/function.array-unique.php

2 Comments

Note from the php docs you linked to: Note that array_unique() is not intended to work on multi dimensional arrays.
0

In your case after you unset element, $i++ in for loop, you reindexed your array and you skip one element. Add $i-- if you unset item. Or you can reindex your array after for loop.

Comments

0

This is also a very simple example you can start improving with.

<?php
$test = ['sample', 'sample', 'sample', 'not', 'not', 'no', 'no'];
$test2 = [];

$k = 0;
foreach ($test as $key => $value) {
  if ($key == 0) {
    $test2[$k] = $value;
    $k++;
  } else {
    if ($test2[$k - 1] != $value) {
      $test2[$k] = $value;
      $k++;
    }
  }
}

$test = $test2;

var_dump($test);

Comments

0

One dirty hack is to check again if you have a duplicate by decreasing $i.

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);
        $i--;
    }
}

This way it will again test your previous value against next item in array.
So if 0==1, then next time if 0==2.
Your code did 0==1 then (2)==(3).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.