我想删除数组中的重复数据。
这是我的数组:
$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)));
不幸的是,不要工作:(
预先感谢,
您的代码可以正常工作,但是主数组中没有重复项。如果您希望一个数组作为输出仅包含整个列表的唯一元素,请查看以下代码:
$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
的重复项已被删除