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

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개

잘 읽었습니다 :)
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
10년 전 조회 1,117
10년 전 조회 1,137
10년 전 조회 1,234
10년 전 조회 1,155
10년 전 조회 1,284
10년 전 조회 1,234
10년 전 조회 1,131
10년 전 조회 1,123
10년 전 조회 1,180
10년 전 조회 1,152
10년 전 조회 1,100
10년 전 조회 1,048
10년 전 조회 1,006
10년 전 조회 1,054
10년 전 조회 1,457
10년 전 조회 1,081
10년 전 조회 1,190
10년 전 조회 1,132
10년 전 조회 1,356
10년 전 조회 1,173
10년 전 조회 1,084
10년 전 조회 1,130
10년 전 조회 1,167
10년 전 조회 1,263
10년 전 조회 1,217
10년 전 조회 1,083
10년 전 조회 1,140
10년 전 조회 2,063
10년 전 조회 1,963
10년 전 조회 1,501
10년 전 조회 2,084
10년 전 조회 1,144
10년 전 조회 1,602
10년 전 조회 1,812
10년 전 조회 1,320
10년 전 조회 1,305
10년 전 조회 1,232
10년 전 조회 1,090
10년 전 조회 1,281
10년 전 조회 1,245
10년 전 조회 1,786
10년 전 조회 1,200
10년 전 조회 1,243
10년 전 조회 1,275
10년 전 조회 2,179
10년 전 조회 1,313
10년 전 조회 1,196
10년 전 조회 1,284
10년 전 조회 1,321
10년 전 조회 1,405
10년 전 조회 1,196
10년 전 조회 1,271
10년 전 조회 1,620
10년 전 조회 1,570
10년 전 조회 1,545
10년 전 조회 1,102
10년 전 조회 1,195
10년 전 조회 1,171
10년 전 조회 1,111
10년 전 조회 1,354
10년 전 조회 1,096
10년 전 조회 1,428
10년 전 조회 1,105
10년 전 조회 1,407
10년 전 조회 1,111
10년 전 조회 1,361
10년 전 조회 1,918
10년 전 조회 1,279
10년 전 조회 1,324
10년 전 조회 1,126
10년 전 조회 2,251
10년 전 조회 1,833
10년 전 조회 1,446
10년 전 조회 1,372
10년 전 조회 1,178
10년 전 조회 1,310
10년 전 조회 2,004
10년 전 조회 2,021
10년 전 조회 2,658
10년 전 조회 1,844
10년 전 조회 1,653
10년 전 조회 2,173
10년 전 조회 1,294
10년 전 조회 1,255
10년 전 조회 1,238
10년 전 조회 1,234
10년 전 조회 1,317
10년 전 조회 1,442
10년 전 조회 1,674
10년 전 조회 1,210
10년 전 조회 1,712
10년 전 조회 1,701
10년 전 조회 1,406
10년 전 조회 1,278
10년 전 조회 1,282
10년 전 조회 1,306
10년 전 조회 1,277
10년 전 조회 1,285
10년 전 조회 1,630
10년 전 조회 1,197