0

I'm looking to create a function that checks an array for duplicate elements, and removes those elements only if the duplicate element is the same as the previous element. Let me give you a simple example:

The following array (in this case, the array will not be multi-dimensional) is as follows:

array('apple', 'apple', 'pear', 'orange', 'banana', 'apple', 'banana');

In this case, the remove duplicate array function, would return an array with the following elements:

array('apple', 'pear', 'orange', 'banana', 'apple', 'banana');

Note that there are still "duplicates" in the array. In case case, the first and second element, were duplicates (both "apple"), with one preceding the other. That said, there are three cases of "apple" found in the first array, but only one (in this case, the second element), was removed, because it was a duplicate of the previous element in the array. Going on those rules, while there are duplicates of "banana", since it isn't a duplicate of the previous array element, it isn't removed. Any ideas on a good function for this?

4 Answers 4

3

Use the following code PHP provide function array_unique to remove duplicates.

$arraywithdups = array('apple', 'apple', 'pear', 'orange', 'banana', 'apple', 'banana');
$arraywithoutdups = array_unique($arraywithdups);

http://sandbox.onlinephpfunctions.com/code/d8a77d1b60b81ff657c002fa1683ed24c73d321a

Sign up to request clarification or add additional context in comments.

Comments

2

something like this perhaps:

function remove_adjacent_dups($arr) {
  $prev = null;
  $result = array();
  foreach( $arr as $val ) {
    if( $val !== $prev ) array_push($result, $val);
    $prev = $val;
  }
  return $result;
}

Comments

0

A version of this, depending on what you want to do with the keys.

$array = array('apple', 'apple', 'apple', 'apple', 'pear', 'orange', 'banana', 'apple', 'banana');
for ($i = 0; $i < count($array); $i++) {
    if ($array[$i] == $array[$i+1]) {
        unset($array[$i]);
    }
}
print_r($array);
//Array ( [3] => apple [4] => pear [5] => orange [6] => banana [7] => apple [8] => banana ) 

1 Comment

This worked as well, and was a bit different than a way that I accepted (which means I learned something new!). I accepted the other answer because 1.) It did what I needed and 2.) Was submitted first. Thanks so much for the help!
0

You could try this:

function removeDuplicates($dupeArray)
{
    $prevElement = NULL;

    foreach($dupeArray as $index => $element)
    {
        if ($prevElement && $prevElement == $element)
        {
            array_splice($dupeArray, $index, 1);
        }
        $prevElement = $element;
    }

    return $dupeArray;
}

$dupeArray = array('apple', 'apple', 'pear', 'orange', 'banana', 'apple', 'banana');
$newArray = removeDuplicates($dupeArray);

var_dump($newArray);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.