删除多维数组中的重复数据

问题描述 投票:1回答:1

我想删除数组中的重复数据。

这是我的数组:

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

我的多维数组:

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
        )

)

这就是我的代码,尝试删除重复的数据:

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

不幸的是,不要工作:(

预先感谢,

php multidimensional-array
1个回答
1
投票

您的代码可以正常工作,但是主数组中没有重复项。如果您希望一个数组作为输出仅包含整个列表的唯一元素,请查看以下代码:

$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);

$unique的输出:

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

pomme的重复项已被删除

© www.soinside.com 2019 - 2024. All rights reserved.