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,497
10년 전 조회 1,464
10년 전 조회 1,554
10년 전 조회 1,463
10년 전 조회 1,650
10년 전 조회 1,551
10년 전 조회 1,470
10년 전 조회 1,469
10년 전 조회 1,494
10년 전 조회 1,489
10년 전 조회 1,426
10년 전 조회 1,378
10년 전 조회 1,358
10년 전 조회 1,398
10년 전 조회 1,788
10년 전 조회 1,427
10년 전 조회 1,519
10년 전 조회 1,463
10년 전 조회 1,704
10년 전 조회 1,519
10년 전 조회 1,441
10년 전 조회 1,469
10년 전 조회 1,500
10년 전 조회 1,625
10년 전 조회 1,546
10년 전 조회 1,444
10년 전 조회 1,481
10년 전 조회 2,433
10년 전 조회 2,296
10년 전 조회 1,862
10년 전 조회 2,437
10년 전 조회 1,511
10년 전 조회 1,905
10년 전 조회 2,147
10년 전 조회 1,637
10년 전 조회 1,580
10년 전 조회 1,556
10년 전 조회 1,415
10년 전 조회 1,592
10년 전 조회 1,589
10년 전 조회 2,110
10년 전 조회 1,524
10년 전 조회 1,590
10년 전 조회 1,598
10년 전 조회 2,521
10년 전 조회 1,647
10년 전 조회 1,535
10년 전 조회 1,624
10년 전 조회 1,658
10년 전 조회 1,736
10년 전 조회 1,535
10년 전 조회 1,605
10년 전 조회 1,953
10년 전 조회 1,905
10년 전 조회 1,881
10년 전 조회 1,438
10년 전 조회 1,540
10년 전 조회 1,491
10년 전 조회 1,450
10년 전 조회 1,674
10년 전 조회 1,443
10년 전 조회 1,744
10년 전 조회 1,433
10년 전 조회 1,766
10년 전 조회 1,461
10년 전 조회 1,688
10년 전 조회 2,293
10년 전 조회 1,601
10년 전 조회 1,650
10년 전 조회 1,521
10년 전 조회 2,576
10년 전 조회 2,184
10년 전 조회 1,798
10년 전 조회 1,723
10년 전 조회 1,555
10년 전 조회 1,664
10년 전 조회 2,332
10년 전 조회 2,399
10년 전 조회 3,048
10년 전 조회 2,149
10년 전 조회 2,036
10년 전 조회 2,529
10년 전 조회 1,658
10년 전 조회 1,622
10년 전 조회 1,627
10년 전 조회 1,624
10년 전 조회 1,689
10년 전 조회 1,792
10년 전 조회 2,026
10년 전 조회 1,581
10년 전 조회 2,071
10년 전 조회 2,010
10년 전 조회 1,756
10년 전 조회 1,631
10년 전 조회 1,609
10년 전 조회 1,665
10년 전 조회 1,620
10년 전 조회 1,613
10년 전 조회 1,955
10년 전 조회 1,533