1

I want to remove duplicate data in my array.

That's my array :

$monTableau = array (
  array("pomme","noix de coco","pêche"),
  array("fraise","pomme", "framboise"),
  array("ananas","citron","raisin"),
  array("pêche","pruneau","pomme")
);

My multidimensionnal array :

Array
(
    [0] => Array
        (
            [0] => pomme
            [1] => noix de coco
            [2] => pêche
        )

    [1] => Array
        (
            [0] => fraise
            [1] => pomme
            [2] => framboise
        )

    [2] => Array
        (
            [0] => ananas
            [1] => citron
            [2] => raisin
        )

    [3] => Array
        (
            [0] => pêche
            [1] => pruneau
            [2] => pomme
        )

)

and that's my code to try remove duplicate data:

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

Don't work unfortunately :(

Advance Thanks,

2
  • Can you post an array we can work with? Maybe the unserialiezd one Commented May 14, 2020 at 9:30
  • Sorry, yes of course! Commented May 14, 2020 at 9:35

1 Answer 1

1

Your code does work properly, but there is no duplicates in the main array. If you want one single array as output that contains only unique elements of the whole list take a look at this code:

$monTableau = array (
  array("pomme","noix de coco","pêche"),
  array("fraise","pomme", "framboise"),
  array("ananas","citron","raisin"),
  array("pêche","pruneau","pomme")
);


$merged = call_user_func_array('array_merge', $monTableau);
$unique = array_unique($merged);

Output of $unique:

Array
(
    [0] => pomme
    [1] => noix de coco
    [2] => pêche
    [3] => fraise
    [5] => framboise
    [6] => ananas
    [7] => citron
    [8] => raisin
    [10] => pruneau
)

Duplicates of pomme have been removed

Sign up to request clarification or add additional context in comments.

Comments

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.