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,419
10년 전 조회 1,404
10년 전 조회 1,484
10년 전 조회 1,396
10년 전 조회 1,591
10년 전 조회 1,486
10년 전 조회 1,405
10년 전 조회 1,411
10년 전 조회 1,451
10년 전 조회 1,432
10년 전 조회 1,353
10년 전 조회 1,312
10년 전 조회 1,287
10년 전 조회 1,326
10년 전 조회 1,722
10년 전 조회 1,348
10년 전 조회 1,460
10년 전 조회 1,408
10년 전 조회 1,644
10년 전 조회 1,447
10년 전 조회 1,382
10년 전 조회 1,394
10년 전 조회 1,429
10년 전 조회 1,548
10년 전 조회 1,492
10년 전 조회 1,382
10년 전 조회 1,419
10년 전 조회 2,360
10년 전 조회 2,222
10년 전 조회 1,811
10년 전 조회 2,371
10년 전 조회 1,443
10년 전 조회 1,845
10년 전 조회 2,076
10년 전 조회 1,564
10년 전 조회 1,518
10년 전 조회 1,492
10년 전 조회 1,344
10년 전 조회 1,525
10년 전 조회 1,536
10년 전 조회 2,040
10년 전 조회 1,474
10년 전 조회 1,525
10년 전 조회 1,538
10년 전 조회 2,451
10년 전 조회 1,573
10년 전 조회 1,462
10년 전 조회 1,552
10년 전 조회 1,600
10년 전 조회 1,678
10년 전 조회 1,475
10년 전 조회 1,526
10년 전 조회 1,876
10년 전 조회 1,828
10년 전 조회 1,811
10년 전 조회 1,373
10년 전 조회 1,473
10년 전 조회 1,435
10년 전 조회 1,393
10년 전 조회 1,612
10년 전 조회 1,381
10년 전 조회 1,684
10년 전 조회 1,362
10년 전 조회 1,701
10년 전 조회 1,391
10년 전 조회 1,627
10년 전 조회 2,221
10년 전 조회 1,536
10년 전 조회 1,599
10년 전 조회 1,446
10년 전 조회 2,518
10년 전 조회 2,136
10년 전 조회 1,731
10년 전 조회 1,655
10년 전 조회 1,486
10년 전 조회 1,596
10년 전 조회 2,265
10년 전 조회 2,333
10년 전 조회 2,973
10년 전 조회 2,091
10년 전 조회 1,961
10년 전 조회 2,466
10년 전 조회 1,589
10년 전 조회 1,553
10년 전 조회 1,537
10년 전 조회 1,551
10년 전 조회 1,617
10년 전 조회 1,714
10년 전 조회 1,943
10년 전 조회 1,492
10년 전 조회 2,008
10년 전 조회 1,942
10년 전 조회 1,687
10년 전 조회 1,557
10년 전 조회 1,539
10년 전 조회 1,600
10년 전 조회 1,554
10년 전 조회 1,552
10년 전 조회 1,887
10년 전 조회 1,466