I'm implementing manually the array_unique
function. I'm doing this:
class Prelude {
public static function unique($xs) {
$arr = array();
foreach($xs as $atom) {
if (!array_search($atom, $arr)) {
var_dump($arr);
echo "<br>";
array_push($arr, $atom);
}
}
return $arr;
}
}
Prelude :: unique(array(1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 6, 7, 8));
I noticed that it works very well, except when $atom
is 1
. In my foreach
loop, when I check if the value is already contained in the array and the value I want to check is one, it simply ignores and says me that there is no 1
in my array. Here is my output:
And here is when I check if there is 1
in [1, 1, 1]
:
Can somebody explain me this?
Simple, Marcelo.
You're checking with !
which is the non-strict checking. You must check for !== FALSE. 1 is TRUE.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다