테스트 사이트 - 개발 중인 베타 버전입니다

php in_array 사용법 배열값 확인하기

· 10년 전 · 566 · 1

in_array

 

(PHP 4, PHP 5, PHP 7)

in_array — 값이 배열 안에 존재하는지 확인

 

 

설명 ¶

 

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

 

 

인수 ¶

 

needle

찾을 값.

 

Note:

needle이 문자열이면, 대소문자를 구분하여 비교합니다.

haystack

배열.

 

strict

세번째 인수 strict를 TRUE로 설정하면, in_array() 함수는 haystack 안에서 needle의 자료형도 확인합니다.

 

 

반환값 ¶

 

needle을 배열에서 찾으면 TRUE를, 아니면 FALSE를 반환합니다.

 

 

Example #1 in_array() 예제

 

<?php

$os = array("Mac", "NT", "Irix", "Linux");

if (in_array("Irix", $os)) {

    echo "Got Irix";

}

if (in_array("mac", $os)) {

    echo "Got mac";

}

?>

in_array()가 대소문자를 구분하므로 두번째 조건은 실패하고, 위 프로그램은 다음을 출력합니다:

 

output : 

 

Got Irix

 

 

 

Example #2 in_array()에 strict 예제

 

<?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

Example #3 in_array()에 needle로 배열

 

<?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";

}

?>

위 예제의 출력:

 

  'ph' was found

  'o' was found 



위 예제는 php.net에서 가져온것이고 거기에 제 설명을 간미한것입니다.

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 1개

잘 읽었습니다 :)

게시글 목록

번호 제목
20097
20095
20093
20092
20091
20089
20086
20085
20084
20083
20082
28358
20081
20079
20078
20077
20076
20075
20074
20073
20072
20071
20070
20069
8148
20057
8141
24669
20056
8138
28353
20055
20054
20053
20052
20051
20050
20047
20046
20045
20044
8111
20043
20042
20040
20039
20037
20036
20035
20027
8107
31022
20026
20025
20024
20023
28350
20022
20018
20017
8099
20016
20013
8090
20012
20010
20009
19998
28348
28347
19997
19996
19995
19993
8087
8065
19990
8063
8056
19989
19988
19987
19986
19985
19984
19983
19982
8054
19981
19980
19979
19978
19977
19976
19975
19974
19973
19972
19971
19970