0

all I want to remove the duplicate value from this Array

Array
(
    [0] => Array
        (
            [0] => Ajay Patel
            [1] => Tag 1
        )

    [1] => Array
        (
            [0] => Tag 1
            [1] => Tag 3
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
        )

)

I tried this solution from How to remove duplicate values from a multi-dimensional array in PHP

$result2 = array_map("unserialize", array_unique(array_map("serialize", $result2)));

But i think something is wrong here, i am getting this as result.

Array
(
    [0] => Array
        (
            [0] => Ajay Patel
            [1] => Tag 1
        )

    [1] => Array
        (
            [0] => Tag 1
            [1] => Tag 3
        )

    [2] => Array
        (
        )

)

What i want is

Array
            (
                [0] => Ajay Patel
                [1] => Tag 1
                [2] => Tag 3
            )

Tag 1 is removed because its 2 times...

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ajay Patel
  • 5,298
  • 11
  • 55
  • 87
  • 1
    @silly can't you see the "Tag 1" two times, please understand the question first. – Ajay Patel Feb 23 '12 at 08:08
  • please post the results-that you expect – silly Feb 23 '12 at 08:10
  • 1
    Ajay, that is an array of arrays. the arrays present in the main array are unique to each other, even if they hold some common values. – Andrei G Feb 23 '12 at 08:10
  • @AndreiG, in that link i have posted in question have the same situation. so what is another way to solve it ? – Ajay Patel Feb 23 '12 at 08:12
  • @silly i wrote completely that what i need.... – Ajay Patel Feb 23 '12 at 08:12
  • Try one of the uncountable other solutions proposed for this problem, like [php multi-dimensional array remove duplicate](http://stackoverflow.com/questions/1861682/php-multi-dimensional-array-remove-duplicate) – deceze Feb 23 '12 at 08:14
  • I don't think it's the same situation, as they are removing arrays that have the same keys. I'll try and adapt for your case and post an asnwer – Andrei G Feb 23 '12 at 08:20
  • 1
    Ajay, please post the exact result you are expecting in the question (marked as code) so that we can see what you want to achieve – Andrei G Feb 23 '12 at 08:21
  • @AndreiG have look at result what i need – Ajay Patel Feb 23 '12 at 08:27

3 Answers3

2

try this

$result = array();
function merge_values(array &$array, $mixed) {
    if(is_array($mixed)) {
        foreach($mixed as $tags) {
            merge_values($array, $tags);
        }
    }
    else {
        if(null !== $mixed && strlen($mixed) > 0 && false === array_search($mixed, $array)) {
            $array[] = $mixed;
        }
    }
}

merge_values($result, $array);
print_r($result);
silly
  • 7,789
  • 2
  • 24
  • 37
2
$result2 = array_unique(call_user_func_array('array_merge',$result2));

In modern PHP, the same technique can be written as:

$result2 = array_unique(array_merge(...$result2));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Lake
  • 813
  • 4
  • 6
  • Thanks! it works for me but logically i am not getting clear how it works.BTW Thanks – Ajay Patel Feb 23 '12 at 08:33
  • The inner part (i.e. call_user_func_array('array_merge', $result2)) merges the sub-arrays into one flat array. The outer array_unique() call removes the duplicate values. This is a nice solution for two-dimensional arrays but will not scale for n-dimensional arrays. Which your question originally referred to. So I'm not quite sure if this really solves your problem or if your question's title is wrong. – nem75 Feb 23 '12 at 09:07
0

I think you should try this

function uniqueElements($outerArray){
    $result=array();
    foreach ($outerArray as $innerArray){
      $result=array_merge($innerArray);
    }

return array_unique($result);
}
Andrei G
  • 1,590
  • 8
  • 13
  • Please prove that this answer works as required by providing a 3v4l.org link. That `array_merge()` looks dead wrong to me. – mickmackusa Dec 28 '21 at 13:38