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 )
这些方法适用于处理大量数据,可以根据具体需求选择合适的方法。