language/php

php 배열에 값이 존재하는 지 확인하는 함수 in_array()

moonsun623 2022. 3. 24. 00:06
반응형

in_array(mixed $needle, array $haystack, bool $stritct = false) 

 

설명 :

배열(haystack)안에 해당 값(needle)이 존재 하는 지 확인 하는 함수로 return 값은 bool 타입

세번째 파라미터인 strict의 default값은 false로 true로 지정할 시, 값의 타입까지 일치해야 true를 리턴한다.

 

ex) 

<?php
$arr = array('1', 2, 3);

if (in_array('2', $arr, true)) {
    echo "'2' found with strict check\n";
}

if (in_array(3, $a, true)) {
    echo "3 found with strict check\n";
}
?>

//out
//3 found with strict check