可以说我有一个数组如下:
$sampArray = array (1,4,2,1,6,4,9,7,2,9)
Run Code Online (Sandbox Code Playgroud)
我想删除此数组中的所有重复项,因此结果应如下所示:
$resultArray = array(1,4,2,6,9,7)
Run Code Online (Sandbox Code Playgroud)
但这是抓住!!! 我不想在内置函数中使用任何PHP array_unique().
你会怎么做?:)
这是一个简单的O(n)-time解决方案:
$uniqueme = array();
foreach ($array as $key => $value) {
$uniqueme[$value] = $key;
}
$final = array();
foreach ($uniqueme as $key => $value) {
$final[] = $key;
}
Run Code Online (Sandbox Code Playgroud)
您不能拥有重复的密钥,这将保留订单.