我正在循环遍历目录中的文件,并将名称和哈希保存在数组中,如下所示:
$localfileslist = [];
$localfiles = glob($_GET['name'].'/*');
foreach($localfiles as $localfile){
if(is_file($localfile)){
$localfilehash= hash_file('sha256', $localfile);
array_push($localfileslist,$localfile, $localfilehash);
}
}
$uniques= array_unique($localfileslist);
$dupes=array_diff_assoc($localfileslist,$aunique);
print_r($result);现在,我很困惑如何继续寻找和删除它们,任何帮助都是感激的。
发布于 2022-09-03 22:03:56
基本上,我首先将每个散列和名称存储在一起。然后,我对散列上的数组进行分组,将所有相同的散列名称推送给每个组。最后,我们可以过滤该分组数组,该数组只有一个以上名称的项。
$localfileslist = [];
$localfiles = glob(DEFINE_PATH . '/*');
foreach ($localfiles as $localfile) {
if (is_file($localfile)) {
$localfilehash = hash_file('sha256', $localfile);
array_push($localfileslist, ['name' => $localfile, 'hash' => $localfilehash]);
}
}
$grouped = array_reduce($localfileslist, function ($agg, $item) {
if (!isset($agg[$item['hash']])) {
$agg[$item['hash']] = [];
}
$agg[$item['hash']][] = $item["name"];
return $agg;
}, []);
$duplicates = array_filter($grouped, function ($item) {
return count($item) > 1;
});
print_r($duplicates);https://stackoverflow.com/questions/73584695
复制相似问题