对于这种情况,您通常需要定义一些逻辑,以根据记录中的值创建哈希以确定相等性。定义好之后,您可以使用简单的循环和关联数组来跟踪哪些记录有重复。
<?php
/**
* Define an algorithm for equality between records.
*
* @param $record
* @return string
*/
function generateHashForUserRecord($record)
{
return sha1($record['Name']);
}
$names = [
['Name' => 'John Smith', 'ID' => 65],
['Name' => 'Richard Johnson', 'ID' => 96],
['Name' => 'John Smith', 'ID' => 1105]
];
// This map will be an populated with all records, keyed by hash
$hashBuffer = [];
// Buffer for hashes that are associated with more than one record
$duplicateHashes = [];
// This will be populated with the duplicate records
$duplicateRecords = [];
// Iterate through all of the records
foreach($names as $currRecord)
{
// Generate a has for the record
$currHash = generateHashForUserRecord($currRecord);
// If the hash is not in the hashtable yet, create an array to hold entries with this hash
if(!array_key_exists($currHash, $hashBuffer))
{
$hashBuffer[$currHash] = [];
}
else // If this hash is already in the buffer, we have a duplicate - add it to the $duplicateHashes array
{
$duplicateHashes[$hash] = $currHash;
}
// Add the record to the hash buffer
$hashBuffer[$currHash][] = $currRecord;
}
foreach($duplicateHashes as $currDuplicateHash)
{
$duplicateRecords = array_merge($duplicateRecords, $hashBuffer[$currDuplicateHash]);
}
print_r($duplicateRecords);
这是很多丑陋的过程代码,所以将它封装在某种帮助类中可能是个好主意。
<?php
$names = [
['Name' => 'John Smith', 'ID' => 65],
['Name' => 'Richard Johnson', 'ID' => 96],
['Name' => 'John Smith', 'ID' => 1105]
];
$duplicateRecords = UserRecordHelper::getDuplicateRecords($names);
print_r($duplicateRecords);
class UserRecordHelper
{
public static function getDuplicateRecords($records)
{
// This map will be an populated with all records, keyed by hash
$hashBuffer = [];
// Buffer for hashes that are associated with more than one record
$duplicateHashes = [];
// This will be populated with the duplicate records
$duplicateRecords = [];
// Iterate through all of the records
foreach ($records as $currRecord)
{
// Generate a has for the record
$currHash = self::generateHashForUserRecord($currRecord);
// If the hash is not in the hashtable yet, create an array to hold entries with this hash
if (!array_key_exists($currHash, $hashBuffer))
{
$hashBuffer[$currHash] = [];
}
else // If this hash is already in the buffer, we have a duplicate - add it to the $duplicateHashes array
{
$duplicateHashes[$hash] = $currHash;
}
// Add the record to the hash buffer
$hashBuffer[$currHash][] = $currRecord;
}
foreach ($duplicateHashes as $currDuplicateHash)
{
$duplicateRecords = array_merge($duplicateRecords, $hashBuffer[$currDuplicateHash]);
}
return $duplicateRecords;
}
public static function generateHashForUserRecord($record)
{
return sha1($record['Name']);
}
}