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數組去重方法,根據實際情況選擇適合的方法即可。