数组去重 发表于 2020-12-05 更新于 2023-05-29 分类于 前端 , JavaScript JS 版本数组去重123let arr = [2, 22, 2, 2];let x = new Set(arr);console.log([...x]); PHP 版本数组去重12345678$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 返回数组的值并建立数字索引.