in_array

(PHP 4 , PHP 5)

in_array -- 배열에서 값이 존재하는지 점검한다

설명

bool in_array ( mixed needle, array haystack [, bool strict])

haystack에서 needle을 검색해서 배열안에서 발견되면 TRUE를 반환하고 그렇지 않으면 FALSE를 반환한다.

세번째 매개변수 strictTRUE로 설정되면, in_array() 함수는 haystack 안의 needle타입도 점검할것이다.

참고: needle이 문자열이면, 대소문자를 구별하여 비교가 수행된다.

참고: PHP 4.2.0 이전 버전에서는 needle은 배열에서 허용되지 않았다.

예 1. in_array() 예제코드

<?php
$os
= array ("Mac", "NT", "Irix", "Linux");
if (
in_array ("Irix", $os)) {
    print
"Got Irix";
}
if (
in_array ("mac", $os)) {
    print
"Got mac";
}
?>

in_array()는 대소문자를 구별하기 때문에 두번째 조건은 실패한다. 그래서 위 프로그램은 다음과 같이 출력된다:

Got Irix

예 2. in_array()의 엄격한 예제코드

<?php
$a
= array('1.10', 12.4, 1.13);

if (
in_array('12.4', $a, TRUE)) {
    echo
"'12.4' found with strict check\n";
}

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

위 코드는 다음과 같이 출력된다:

1.13 found with strict check

예 3. needle 이 배열인 in_array()

<?php
$a
= array(array('p', 'h'), array('p', 'r'), 'o');

if (
in_array(array ('p', 'h'), $a)) {
    echo
"'ph' was found\n";
}

if (
in_array(array ('f', 'i'), $a)) {
    echo
"'fi' was found\n";
}

if (
in_array('o', $a)) {
    echo
"'o' was found\n";
}

/* Outputs:
  'ph' was found
  'o' was found
*/
?>

array_search(), array_key_exists(), isset() 참고.