我有一个这样的数组:
Array
(
[0] => Array
(
[fileid] => 7
[remarks] =>
[filename] => whomovedmycheese - Copy.pdf
[type] => application/pdf
[size] => 500624
)
[1] => Array
(
[fileid] => 7
[remarks] =>
[filename] => whomovedmycheese.pdf
[type] => application/pdf
[size] => 500624
)
[2] => Array
(
[fileid] => 8
[remarks] =>
[filename] => whomovedmycheese - Copy.pdf
[type] => application/pdf
[size] => 500624
)
[3] => Array
(
[fileid] => 8
[remarks] =>
[filename] => whomovedmycheese.pdf
[type] => application/pdf
[size] => 500624
)
)
这是我要删除重复的数组。我尝试使用下面的代码删除重复的内容。
$input4 = array_map("unserialize", array_unique(array_map("serialize", $basicinfo3)));
$new_data3 = array_values($input4);
但结果仍然与起始数组相同。在上面的代码中,$basicinfo
具有上面数组的值。我想要做的是删除重复数组,使其如下所示。
Array
(
[0] => Array
(
[fileid] => 7
[remarks] =>
[filename] => whomovedmycheese - Copy.pdf
[type] => application/pdf
[size] => 500624
)
[1] => Array
(
[fileid] => 7
[remarks] =>
[filename] => whomovedmycheese.pdf
[type] => application/pdf
[size] => 500624
)
)
UPDATE
Array
(
[008-408-601-000-0] => Array
(
[id] => 008-408-601-000
[lastname] => Smith
[firstname] => John
[email] => John@yahoo.com
[Address] => Array
(
[0] => Array
(
[Language] => C++
)
[1] => Array
(
[Language] => C++
)
[2] => Array
(
[Language] => C++
)
[3] => Array
(
[Language] => Java
)
[4] => Array
(
[Language] => Java
)
[5] => Array
(
[Language] => Java
)
)
)
[002-408-641-000-0] => Array
(
[id] => 008-408-601-000
[lastname] => Smith
[firstname] => Jack
[email] => Jack@yahoo.com
[Address] => Array
(
[0] => Array
(
[Language] => C++
)
[1] => Array
(
[Language] => C++
)
[2] => Array
(
[Language] => C++
)
[3] => Array
(
[Language] => Java
)
[4] => Array
(
[Language] => Java
)
[5] => Array
(
[Language] => Java
)
)
)
[001-002-003-000-0] => Array
(
[id] => 008-408-601-000
[lastname] => Smith
[firstname] => Jean
[email] => Jean@yahoo.com
[Address] => Array
(
[0] => Array
(
[Language] => C++
)
[1] => Array
(
[Language] => C++
)
[2] => Array
(
[Language] => C++
)
[3] => Array
(
[Language] => Java
)
[4] => Array
(
[Language] => Java
)
[5] => Array
(
[Language] => Java
)
)
)
)
答案 0 :(得分:1)
您不能使用上面的那个函数,因为这会考虑整个数组的唯一性,正如您已经看到的,某些fileid
已经与相同的文件名不同。如果您需要基于filename
,那么正常循环就足够了。
在您的示例输出中,似乎首先找到的是优先级,然后简单的isset
应该没问题。
$input4 = array();
foreach($basicinfo3 as $values) {
if(!isset($input4[$values['filename']])) {
$input4[$values['filename']] = $values; // if doesn't exist yet, push it
}
}
$input4 = array_values($input4); // simple reindex
答案 1 :(得分:0)
$newArray = array_unique($oldArray, SORT_REGULAR);
请参阅:PHP - Merging two array into one array (also Remove Duplicates)
答案 2 :(得分:0)
您可以使用常规的php数组来实现此目的。
<?php
$data = Array
(
0 => Array
(
"fileid" => 7,
"remarks" => '',
"filename" => "whomovedmycheese - Copy.pdf",
"type" => "application/pdf",
"size" => "500624",
),
1 => Array
(
"fileid"=> 7,
"remarks" => '',
"filename"=> "whomovedmycheese.pdf",
"type"=> "application/pdf",
"size" => "500624",
),
2 => Array
(
"fileid"=> 8,
"remarks"=> '',
"filename"=> "whomovedmycheese - Copy.pdf",
"type"=> "application/pdf",
"size" => "500624",
),
3 => Array
(
"fileid"=> 8,
"remarks"=> '',
"filename"=> "whomovedmycheese.pdf",
"type"=> "application/pdf",
"size" => "500624",
)
);
$newArray = array();
foreach ($data as $file)
{
$newArray[$file['filename']] = $file;
}
echo "<pre>";
print_r($newArray);
?>