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?

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74

5 Answers5

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]);
    }
}
bumperbox
  • 10,166
  • 6
  • 43
  • 66
  • 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. – Paul Dessert Feb 13 '18 at 08:38
  • Strange. My code is acting differently. I'll double check my array. Thanks for the help! – Paul Dessert Feb 13 '18 at 08:55
  • I forgot to reindex the array. `$items = array_values($items);` Working now. – Paul Dessert Feb 13 '18 at 09:01
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

hoyeonUm
  • 49
  • 7
  • Note from the php docs you linked to: Note that array_unique() is not intended to work on multi dimensional arrays. – bumperbox Feb 13 '18 at 07:56
  • oaky, how about https://www.phpflow.com/php/remove-duplicates-from-multidimensional-array/ link ? – hoyeonUm Feb 13 '18 at 08:09
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).

Shobi
  • 10,374
  • 6
  • 46
  • 82
Andreas
  • 23,610
  • 6
  • 30
  • 62
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.

vpalade
  • 1,427
  • 1
  • 16
  • 20
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);
Fil
  • 8,225
  • 14
  • 59
  • 85