2

I found many question and answer about this, but not match what i need. i think it's similar/same to this question but don't know why not working for this case. So please try before judging duplicates, thank you.

array source

$avar = array(
0 => array(1,2,3,4,5,6,7,8,9),
1 => array(10,11,12,13,14,15,16,17,7,8,9,10),
23 => array(21,22,23,4,5,6,7,11,12,13,14,15,21));

desired result

$avar = array(
0 => array(1,2,3,4,5,6,7,8,9),
1 => array(10,11,12,13,14,15,16,17),
23 => array(21,22,23));

PHP script

<?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;
}

$result = super_unique($avar);  
echo "<pre>";
print_r($result);

?>

similar question with answer but not solve my case:

  1. How to remove duplicate values from a multi-dimensional array in PHP
  2. PHP remove duplicate values from multidimensional array

Thank you all

2
  • are the array keys (0,1,23) fixed? Commented May 15, 2013 at 1:50
  • No, it's dynamic. pulling from table ID Commented May 15, 2013 at 1:54

1 Answer 1

3
$seen = array();
foreach($avar as &$entry){
    $entry = array_unique(array_diff($entry,$seen));
    $seen = array_merge($entry,$seen);
}
unset($entry);
var_dump($avar);
Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to post exactly the same! :)

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.