1

I have this array :

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 10
                    [id_list] => 1
                [id] => 1
            )

        [1] => Array
            (
                [0] => 11
                [id_list] => 1
                [id] => 1
            )

        [2] => Array
            (
                [0] => 12
                [id_list] => 1
                [id] => 1
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [0] => 11
                [id_list] => 2
                [id] => 2
            )

        [1] => Array
            (
                [0] => 12
                [id_list] => 2
                [id] => 2
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [0] => 13
                [id_list] => 4
                [id] => 4
            )
    )
)

and this code (where $dataListe is the result of a fetchAll query) :

    $result = [];
    foreach($dataListe as $listeDiff){
       $result[] = $listeDiff;
    }
    // $resultUnique = array_unique($result);
    echo "<pre>".print_r($result, true)."</pre>";

as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).

I want to remove the duplicate of each element in the general array.

But when I use array unique, I get this result :

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 10
                    [id_list] => 1
                    [id] => 1
                )
            [1] => Array
                (
                    [0] => 11
                    [id_list] => 1
                    [id] => 1
                )
            [2] => Array
                (
                    [0] => 12
                    [id_list] => 1
                    [id] => 1
                )
        )
) 

Please I need help to only keep 1 item of each array at the end !

EDIT : I have almost the good result with the code below, but the id 12 is missing

            $result = [];
        foreach($dataListe as $listeDiff){
            foreach($listeDiff as $contact){
                if(!in_array($contact,$result)){
                    $result[] = $contact;
                }
                break;
            }
        }

1 Answers1

0

As the PHP docs says :

Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)

You can try this solution

$uniqueResult = array_map("unserialize", array_unique(
    array_map("serialize", $result)
));

as suggested by @daveilers on this question How to remove duplicate values from a multi-dimensional array in PHP.

Grégory
  • 338
  • 2
  • 10