PHP大數據去重的方法有多種,以下是一些常用的方法:
$data = [1, 2, 3, 2, 4, 5, 1];
$result = array_unique($data);
print_r($result); // 輸出:Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 4 [5] => 5 )
$data = [1, 2, 3, 2, 4, 5, 1];
$connection = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
$connection->exec("CREATE TABLE IF NOT EXISTS mytable (id INT PRIMARY KEY)");
foreach ($data as $value) {
$connection->exec("INSERT IGNORE INTO mytable (id) VALUES ($value)");
}
$result = $connection->query("SELECT id FROM mytable")->fetchAll(PDO::FETCH_COLUMN);
print_r($result); // 輸出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
$data = [1, 2, 3, 2, 4, 5, 1];
$result = [];
foreach ($data as $value) {
$result[hash('sha256', $value)] = $value;
}
$result = array_values($result);
print_r($result); // 輸出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
這些方法適用于處理大量數據,可以根據具體需求選擇合適的方法。