나는이 다음 코드 :이상한 array_search 동작
<?php
$ray = array(1, "aa" , 0);
echo "Index = " . array_search("I want to find this text", $ray);
?>
어떻게 설명 할 array_search()
함수가 리턴 인덱스 2를 기존의 것을?
나는이 다음 코드 :이상한 array_search 동작
<?php
$ray = array(1, "aa" , 0);
echo "Index = " . array_search("I want to find this text", $ray);
?>
어떻게 설명 할 array_search()
함수가 리턴 인덱스 2를 기존의 것을?
array_search
은 ==
을 사용하여 비교합니다. 그러면 PHP 이 피연산자로 변환되어 해당 유형이 일치합니다.
1 == "I want to find this text"
"aa" == "I want to find this text"
0 == "I want to find this text"
1과 3의 경우 PHP는 "I want to find this text"
을 비교할 수 있도록 숫자로 변환해야합니다. 문자열을 숫자로 변환 할 때 PHP는 문자열의 처음부터 읽고 첫 번째 숫자가 아닌 문자에서 멈 춥니 다. 따라서 "I want to find this text"
은 0
으로 변환됩니다.
2를 얻을 그 이유는,
1 == "I want to find this text" => 1 == 0 => false
"aa" == "I want to find this text" => false
0 == "I want to find this text" => 0 == 0 => true
하고,이 작업을 수행 : array_search("I want to find this text", $ray, true)
는
제 3 매개 변수 대신 ===
를 사용하는 array_search
을 알려줍니다. 이것은 이 아니고 유형을 변환하는 대신에 그것도 비교합니다. 그러면 FALSE
은 "I want to find this text"
과 일치하지 않으므로 에 유형 및 값으로 표시됩니다.