PHP数组去重的方法有以下几种:
$array = array(1, 2, 2, 3, 4, 4);
$newArray = array_unique($array);
print_r($newArray);
输出:Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 )
$array = array(1, 2, 2, 3, 4, 4);
$newArray = array_flip(array_flip($array));
print_r($newArray);
输出:Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 )
$array = array(1, 2, 2, 3, 4, 4);
$newArray = array();
foreach ($array as $value) {
if (!in_array($value, $newArray)) {
$newArray[] = $value;
}
}
print_r($newArray);
输出:Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 )
以上是三种常用的PHP数组去重方法,根据实际情况选择适合的方法即可。