数组去重

JS 版本数组去重

1
2
3
let arr = [2, 22, 2, 2];
let x = new Set(arr);
console.log([...x]);

PHP 版本数组去重

1
2
3
4
5
6
7
8
$input = ["a" => "green", "red", "b" => "green", "blue", "red"];
//方法一 使用array_unique()函数
array_unique($input);
print_r($input);
//方法二 键值交换, 然后只取键
$res = array_flip($input);
$res = array_keys($input);
print_r($res);

补充 : 如何重建数组的索引 ?

1
//使用array_values 返回数组的值并建立数字索引.