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,417
10년 전 조회 1,399
10년 전 조회 1,475
10년 전 조회 1,392
10년 전 조회 1,586
10년 전 조회 1,482
10년 전 조회 1,402
10년 전 조회 1,405
10년 전 조회 1,449
10년 전 조회 1,427
10년 전 조회 1,349
10년 전 조회 1,309
10년 전 조회 1,285
10년 전 조회 1,318
10년 전 조회 1,709
10년 전 조회 1,340
10년 전 조회 1,456
10년 전 조회 1,402
10년 전 조회 1,634
10년 전 조회 1,437
10년 전 조회 1,378
10년 전 조회 1,389
10년 전 조회 1,423
10년 전 조회 1,546
10년 전 조회 1,488
10년 전 조회 1,376
10년 전 조회 1,412
10년 전 조회 2,354
10년 전 조회 2,217
10년 전 조회 1,804
10년 전 조회 2,362
10년 전 조회 1,436
10년 전 조회 1,840
10년 전 조회 2,071
10년 전 조회 1,560
10년 전 조회 1,515
10년 전 조회 1,488
10년 전 조회 1,342
10년 전 조회 1,520
10년 전 조회 1,528
10년 전 조회 2,034
10년 전 조회 1,467
10년 전 조회 1,521
10년 전 조회 1,534
10년 전 조회 2,446
10년 전 조회 1,568
10년 전 조회 1,457
10년 전 조회 1,545
10년 전 조회 1,593
10년 전 조회 1,674
10년 전 조회 1,468
10년 전 조회 1,520
10년 전 조회 1,870
10년 전 조회 1,825
10년 전 조회 1,805
10년 전 조회 1,362
10년 전 조회 1,466
10년 전 조회 1,429
10년 전 조회 1,387
10년 전 조회 1,605
10년 전 조회 1,377
10년 전 조회 1,682
10년 전 조회 1,359
10년 전 조회 1,694
10년 전 조회 1,387
10년 전 조회 1,623
10년 전 조회 2,218
10년 전 조회 1,528
10년 전 조회 1,593
10년 전 조회 1,434
10년 전 조회 2,514
10년 전 조회 2,131
10년 전 조회 1,717
10년 전 조회 1,650
10년 전 조회 1,480
10년 전 조회 1,589
10년 전 조회 2,259
10년 전 조회 2,321
10년 전 조회 2,967
10년 전 조회 2,088
10년 전 조회 1,954
10년 전 조회 2,461
10년 전 조회 1,585
10년 전 조회 1,548
10년 전 조회 1,530
10년 전 조회 1,538
10년 전 조회 1,612
10년 전 조회 1,709
10년 전 조회 1,938
10년 전 조회 1,485
10년 전 조회 1,996
10년 전 조회 1,937
10년 전 조회 1,678
10년 전 조회 1,550
10년 전 조회 1,533
10년 전 조회 1,592
10년 전 조회 1,547
10년 전 조회 1,547
10년 전 조회 1,880
10년 전 조회 1,457