2010-03-02 1 views

답변

3

마지막 인수가 true이면 배열을 검색 할 때 strict (also known as identity) comparison (===)을 사용합니다.

동일성 비교 (==)는 ID 비교 (===)가 값과 유형을 비교할 때 값을 비교합니다.

'0' == 0 ; //true, the string is converted to an integer and then the are compared. 
'0' === 0; //false, a string is not equal to a integer 
당신은이 질문에서 더 많은 정보를 찾을 수

How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

이 의미 당신이 false를 반환합니다 문자열 값 '2'에 대한 엄격한 비교를 사용하여 숫자

$a = array(0,1,2,3,4); 

의 배열이 있다면 (일치 항목을 찾지 못했습니다.) 값이 '2' 인 문자열이 없기 때문입니다. 당신이 엄격한 검색을 수행하지 않는 경우

array_search('2', $a, true); //returns false 

그러나, 문자열은 암시 적으로 정수 (또는 그 반대)로 변환하며 2 == '2'

array_search('2', $a, false); //returns 2 
1

로, 2의 인덱스를 반환 세 번째 매개 변수는 바늘과 건초 더미 요소의 유형 (즉 엄격한 비교 ===)을 확인하는 기능을 알려줍니다.

<?php 

$needle = "2"; //a string 
$haystack = array (1,2,"2","test"); 

$search = array_search ($needle ,$haystack, false); 

// Will output 1, as it is the key for the second element of the array (an integer) 
print_r($search); 


$search = array_search ($needle ,$haystack, true); 

//Will output 2, as it is the key for the third element of the array (a string) 
print_r($search); 

?> 
1

array_search에서 세 번째 인수는 엄격한 유형 검사에 사용됩니다.

번째 인자가 거짓이면 (123)는 동일한 행 "123"==> 진정한

번째 인수가 참일 경우

(123)가 동일하지가 "123"==> 모두 다른 종류 이래로.

관련 문제