14

We can use array_unique() for remove duplicate entry from a single multidimensional array in php.Is it possible to use with multidimensional array? It is not working for me!

Here's what the array looks like

Array (
    [0] => Array ( [0] => 1001 [1] => john [2] => example )
    [1] => Array ( [0] => 1002 [1] => test [2] => dreamz )
    [2] => Array ( [0] => 1001 [1] => john [2] => example )
    [3] => Array ( [0] => 1001 [1] => example [2] => john )
    [4] => Array ( [0] => 1001 [1] => john [2] => example )
)

Anybody can please help me...

3
  • must a duplicate of: stackoverflow.com/questions/307674/… or stackoverflow.com/questions/1861682/… Commented Aug 30, 2010 at 6:27
  • 1
    All functions like array_unique() are just a syntax sugar for really simple loops. With very little effort you can do it yourself for sure. Try to be more devious. Programming is not always just copy-pasting, sometimes it require more intelligent work. Commented Aug 30, 2010 at 6:48
  • This question is not a great canonical because it does not explicitly describe what deems a row to be a duplicate. The sample data has fully identical rows, but no partially identical rows, so the requirement is ambiguous. One of the rows has swapped column values, but we don't know what result is expected. Commented Aug 30 at 22:27

2 Answers 2

40

The user comments on the array_unique page do shed some light on this. You will most likely find some hidden gems in those comments - its a very handy documentation.

Just a quick browser through revealed the following to remove duplicates from a multi dimensional array:

<?php
function super_unique($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
  {
    if ( is_array($value) )
    {
      $result[$key] = super_unique($value);
    }
  }

  return $result;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Russell i am able to remove duplicated using this function but have 1 problem how to re index the array inside the array..hope you can shed light
A cleaner solution, taken from stackoverflow.com/questions/307674/…: $input = array_map("unserialize", array_unique(array_map("serialize", $input)));
3

You could serialize the sub-arrays (via serialize()) into a new array, then run array_unique() on that, and then unserialize the resulting set of arrays.

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.