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,322
10년 전 조회 1,313
10년 전 조회 1,413
10년 전 조회 1,316
10년 전 조회 1,479
10년 전 조회 1,395
10년 전 조회 1,319
10년 전 조회 1,317
10년 전 조회 1,373
10년 전 조회 1,346
10년 전 조회 1,262
10년 전 조회 1,222
10년 전 조회 1,192
10년 전 조회 1,227
10년 전 조회 1,614
10년 전 조회 1,267
10년 전 조회 1,370
10년 전 조회 1,329
10년 전 조회 1,546
10년 전 조회 1,351
10년 전 조회 1,303
10년 전 조회 1,306
10년 전 조회 1,334
10년 전 조회 1,455
10년 전 조회 1,387
10년 전 조회 1,288
10년 전 조회 1,327
10년 전 조회 2,258
10년 전 조회 2,139
10년 전 조회 1,706
10년 전 조회 2,261
10년 전 조회 1,326
10년 전 조회 1,764
10년 전 조회 1,988
10년 전 조회 1,466
10년 전 조회 1,435
10년 전 조회 1,405
10년 전 조회 1,267
10년 전 조회 1,440
10년 전 조회 1,438
10년 전 조회 1,947
10년 전 조회 1,376
10년 전 조회 1,430
10년 전 조회 1,456
10년 전 조회 2,359
10년 전 조회 1,499
10년 전 조회 1,381
10년 전 조회 1,469
10년 전 조회 1,511
10년 전 조회 1,596
10년 전 조회 1,386
10년 전 조회 1,448
10년 전 조회 1,785
10년 전 조회 1,742
10년 전 조회 1,716
10년 전 조회 1,270
10년 전 조회 1,395
10년 전 조회 1,350
10년 전 조회 1,292
10년 전 조회 1,516
10년 전 조회 1,297
10년 전 조회 1,603
10년 전 조회 1,278
10년 전 조회 1,594
10년 전 조회 1,289
10년 전 조회 1,533
10년 전 조회 2,120
10년 전 조회 1,458
10년 전 조회 1,521
10년 전 조회 1,337
10년 전 조회 2,422
10년 전 조회 2,025
10년 전 조회 1,623
10년 전 조회 1,551
10년 전 조회 1,378
10년 전 조회 1,510
10년 전 조회 2,182
10년 전 조회 2,220
10년 전 조회 2,859
10년 전 조회 1,994
10년 전 조회 1,850
10년 전 조회 2,362
10년 전 조회 1,484
10년 전 조회 1,467
10년 전 조회 1,433
10년 전 조회 1,451
10년 전 조회 1,512
10년 전 조회 1,625
10년 전 조회 1,845
10년 전 조회 1,395
10년 전 조회 1,890
10년 전 조회 1,844
10년 전 조회 1,588
10년 전 조회 1,455
10년 전 조회 1,440
10년 전 조회 1,496
10년 전 조회 1,475
10년 전 조회 1,461
10년 전 조회 1,799
10년 전 조회 1,361