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년 전 조회 614
10년 전 조회 616
10년 전 조회 722
10년 전 조회 622
10년 전 조회 791
10년 전 조회 711
10년 전 조회 605
10년 전 조회 615
10년 전 조회 664
10년 전 조회 645
10년 전 조회 544
10년 전 조회 538
10년 전 조회 504
10년 전 조회 550
10년 전 조회 958
10년 전 조회 540
10년 전 조회 679
10년 전 조회 577
10년 전 조회 786
10년 전 조회 611
10년 전 조회 503
10년 전 조회 570
10년 전 조회 600
10년 전 조회 719
10년 전 조회 651
10년 전 조회 502
10년 전 조회 579
10년 전 조회 1,534
10년 전 조회 1,409
10년 전 조회 969
10년 전 조회 1,572
10년 전 조회 614
10년 전 조회 1,063
10년 전 조회 1,272
10년 전 조회 730
10년 전 조회 660
10년 전 조회 661
10년 전 조회 512
10년 전 조회 692
10년 전 조회 681
10년 전 조회 1,219
10년 전 조회 633
10년 전 조회 707
10년 전 조회 681
10년 전 조회 1,599
10년 전 조회 714
10년 전 조회 591
10년 전 조회 722
10년 전 조회 715
10년 전 조회 811
10년 전 조회 580
10년 전 조회 684
10년 전 조회 1,039
10년 전 조회 992
10년 전 조회 972
10년 전 조회 513
10년 전 조회 624
10년 전 조회 612
10년 전 조회 553
10년 전 조회 789
10년 전 조회 563
10년 전 조회 869
10년 전 조회 529
10년 전 조회 867
10년 전 조회 545
10년 전 조회 781
10년 전 조회 1,349
10년 전 조회 652
10년 전 조회 684
10년 전 조회 532
10년 전 조회 1,605
10년 전 조회 1,247
10년 전 조회 855
10년 전 조회 786
10년 전 조회 584
10년 전 조회 694
10년 전 조회 1,379
10년 전 조회 1,432
10년 전 조회 2,099
10년 전 조회 1,152
10년 전 조회 1,071
10년 전 조회 1,560
10년 전 조회 698
10년 전 조회 652
10년 전 조회 650
10년 전 조회 646
10년 전 조회 733
10년 전 조회 848
10년 전 조회 1,081
10년 전 조회 612
10년 전 조회 1,131
10년 전 조회 1,012
10년 전 조회 820
10년 전 조회 696
10년 전 조회 662
10년 전 조회 727
10년 전 조회 673
10년 전 조회 696
10년 전 조회 1,039
10년 전 조회 620