我有这个数组:
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
)
)
)
和此代码(其中$ dataListe是fetchAll查询的结果):
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
如您所见,在我的第一个数组和第二个数组中都有一些相似的联系人(但是在第一个和第三个数组中,联系人可以相同,如果我选择在第三个数组中添加我的联系人)。
我想删除通用数组中每个元素的重复项。
但是当我使用唯一数组时,会得到以下结果:
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
)
)
)
请帮助我,使每个数组的末尾仅保留一项!
编辑:使用下面的代码,我几乎得到了很好的结果,但是缺少ID 12
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
答案 0 :(得分:0)
PHP文档说:
注意:请注意,array_unique()不适用于多维数组。 (http://php.net/manual/en/function.array-unique.php)
您可以尝试此解决方案
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
如@daveilers关于这个问题How to remove duplicate values from a multi-dimensional array in PHP的建议。