【发布时间】:2016-06-01 20:31:28
【问题描述】:
我是 PHP 新手,正在尝试删除数组中的重复条目。我最终得到了我想要的输出,但在此过程中我也遇到了两个“未定义的偏移”错误。这是我的代码: $this->master 指的是在类的开头声明的数组。
public function removeDuplicates(){
$var = count($this->master);
for($i = 0; $i < $var; $i++){
for($j = 0; $j <$var; $j++){
if(($this->master[$i] == $this->master[$j]) && $i != $j){
$this->shiftLeft($j, $var);
$var --;
}
}
}
}
public function shiftLeft($t, $s){
while($t < $s){
echo "$t ";
$this->master[$t] = $this->master[$t+1];
$t++;
}
unset($this->master[$t-1]);
}
这可能是一个非常简单的逻辑错误,但我似乎找不到在哪里。非常感谢任何帮助。
【问题讨论】:
-
为什么不使用array_unique? php.net/manual/en/function.array-unique.php
-
是的,它成功了,谢谢。
标签: php arrays duplicates undefined offset