0

how can I check and delete duplicate arrays?

Example:

$a = array(
   array(
      'id' => 1,
      'name' => 'test'
   ),
   // Next array is equal to first, then delete
   array(
      'id' => 1,
      'name' => 'test'
   ), 
   // Different array, then continue here
   array(
      'id' => 2,
      'name' => 'other'
   )
);

If the array is the same, then delete the duplicate and get only one array.

  • http://php.net/manual/en/function.array-unique.php#116302 (this comment has an example function to handle multidimensional arrays) – WOUNDEDStevenJones Nov 20 '18 at 20:10
  • Are you interested in deleting duplicates only if they are adjacent, or are you interested in creating a full unique set? – ggorlen Nov 20 '18 at 20:20

3 Answers3

0

array_unique()

Example:

$array = array(1, 2, 2, 3);
    $array = array_unique($array); // Array is now (1, 2, 3)
  • This works great on an array of primitives, but this [won't work on an array of arrays](https://stackoverflow.com/a/24138391/6243352). – ggorlen Nov 20 '18 at 20:22
0

You can use a lookup table storing serialized arrays. If an array already exists in the lookup table, you have a duplicate and can splice out the key:

$a = array(
   array(
      'id' => 1,
      'name' => 'test'
   ),
   array(
      'id' => 1,
      'name' => 'test'
   ), 
   array(
      'id' => 2,
      'name' => 'other'
   )
);

$seen = [];

for ($i = count($a) - 1; $i >= 0; $i--) {
    if (array_key_exists(json_encode($a[$i]), $seen)) {
        array_splice($a, $i, 1);
    }
    else {
        $seen[json_encode($a[$i])] = 1;
    }
}

print_r($a);

Output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => test
        )

    [1] => Array
        (
            [id] => 2
            [name] => other
        )

)

Try it!

ggorlen
  • 44,755
  • 7
  • 76
  • 106
0

You can loop through the parent array, serialize each child array and save it in a third array, and as you are looping, check for the existence of the serial of each next child array to all previous ones saved in the third array. If it exists, remove the current duplicate from the parent array by key. The function below demonstrates this.

function remove_duplicate_nested_arrays($parent_array)

  $temporary_array = array(); // declare third, temporary array.

  foreach($parent_array as $key =>  $child_array){ // loop through parent array
    $child_array_serial = serialize($child_array); // serialize child each array
    if(in_array($child_array_serial,$temporary_array)){ // check if child array serial exists in third array
      unset($parent_array[$key]); // unset the child array by key from parent array if it's serial exists in third array
      continue;
    }
    $temporary_array[] = $child_array_serial; // if this point is reached, the serial of child array is not in third array, so add it so duplicates can be detected in future iterations.
  }
  return $parent_array;
}

This can also be achieved in 1 line, using @Jose Carlos Gp suggestion as follows:

$b = array_map('unserialize', array_unique(array_map('serialize', $a)));

The function above kind of expands on what is actually happening in the 1 liner solution.

coderodour
  • 1,072
  • 8
  • 16