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,450
10년 전 조회 1,424
10년 전 조회 1,513
10년 전 조회 1,429
10년 전 조회 1,619
10년 전 조회 1,515
10년 전 조회 1,437
10년 전 조회 1,440
10년 전 조회 1,466
10년 전 조회 1,452
10년 전 조회 1,383
10년 전 조회 1,350
10년 전 조회 1,324
10년 전 조회 1,362
10년 전 조회 1,749
10년 전 조회 1,379
10년 전 조회 1,479
10년 전 조회 1,439
10년 전 조회 1,673
10년 전 조회 1,482
10년 전 조회 1,403
10년 전 조회 1,425
10년 전 조회 1,458
10년 전 조회 1,584
10년 전 조회 1,513
10년 전 조회 1,417
10년 전 조회 1,447
10년 전 조회 2,389
10년 전 조회 2,255
10년 전 조회 1,832
10년 전 조회 2,401
10년 전 조회 1,471
10년 전 조회 1,868
10년 전 조회 2,102
10년 전 조회 1,595
10년 전 조회 1,536
10년 전 조회 1,522
10년 전 조회 1,381
10년 전 조회 1,549
10년 전 조회 1,558
10년 전 조회 2,072
10년 전 조회 1,497
10년 전 조회 1,553
10년 전 조회 1,562
10년 전 조회 2,480
10년 전 조회 1,602
10년 전 조회 1,493
10년 전 조회 1,577
10년 전 조회 1,622
10년 전 조회 1,705
10년 전 조회 1,506
10년 전 조회 1,566
10년 전 조회 1,911
10년 전 조회 1,866
10년 전 조회 1,838
10년 전 조회 1,398
10년 전 조회 1,498
10년 전 조회 1,454
10년 전 조회 1,414
10년 전 조회 1,639
10년 전 조회 1,413
10년 전 조회 1,707
10년 전 조회 1,392
10년 전 조회 1,729
10년 전 조회 1,425
10년 전 조회 1,648
10년 전 조회 2,258
10년 전 조회 1,559
10년 전 조회 1,620
10년 전 조회 1,480
10년 전 조회 2,545
10년 전 조회 2,158
10년 전 조회 1,756
10년 전 조회 1,690
10년 전 조회 1,522
10년 전 조회 1,629
10년 전 조회 2,297
10년 전 조회 2,365
10년 전 조회 3,010
10년 전 조회 2,120
10년 전 조회 1,992
10년 전 조회 2,497
10년 전 조회 1,623
10년 전 조회 1,588
10년 전 조회 1,579
10년 전 조회 1,583
10년 전 조회 1,648
10년 전 조회 1,750
10년 전 조회 1,986
10년 전 조회 1,536
10년 전 조회 2,044
10년 전 조회 1,976
10년 전 조회 1,717
10년 전 조회 1,584
10년 전 조회 1,573
10년 전 조회 1,631
10년 전 조회 1,581
10년 전 조회 1,582
10년 전 조회 1,918
10년 전 조회 1,492