유용한 함수 array_filter
(PHP 4 >= 4.0.6, PHP 5)
array_filter — Filters elements of an array using a callback function
배열의 각 원소들을 콜백 함수의 인자로 각각 넣어서 함수의 결과값이 true(1) 인것만 뽑아서 배열로 반환.
array array_filter ( array $input [, callback $callback ] )
예제
function odd($var){
// returns whether the input integer is odd, $var 가 홀수 일경우 1 을 반환, 짝수일 경우 0 을 반환
return($var & 1);
}
function even($var){
// returns whether the input integer is even, $var 가 짝수 일경우 true 를 반환, 홀수 일경우 false 를 반환
return(!($var & 1));
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
결과
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
)
참고 - 콜백함수 란
간단하게 설명하면. 특정 함수 내에서 처리 과정 내에서 자동적으로 호출 되어 사용되는 함수
즉 array_filter 를 직접 구현해 보면
function array_filter_test($array, $function=''){
if (!is_array($array)) return Array();
$return = Array();
foreach($array as $k => $v){
if (!empty($function)) $result = call_user_func($function, $v);//여기서 콜백 함수가 사용됨
else $result = !empty($v);
if (!empty($result)) $return[$k] = $v;
}
return $return;
}
echo "Odd :\n";
print_r(array_filter_test($array1, "odd"));
echo "Even:\n";
print_r(array_filter_test($array2, "even"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7530 | 10년 전 | 756 | ||
| 7529 |
파랑새1597
|
10년 전 | 1192 | |
| 7528 |
파랑새1597
|
10년 전 | 1289 | |
| 7527 |
integrity7
|
10년 전 | 1374 | |
| 7526 | 10년 전 | 2396 | ||
| 7525 |
다빈치코드777
|
10년 전 | 1087 | |
| 7524 | 10년 전 | 1551 | ||
| 7523 | 10년 전 | 953 | ||
| 7522 |
|
10년 전 | 974 | |
| 7521 |
blackkil
|
10년 전 | 1850 | |
| 7520 | 10년 전 | 1271 | ||
| 7519 |
Gaumi
|
10년 전 | 1071 | |
| 7518 | 10년 전 | 1472 | ||
| 7517 | 10년 전 | 808 | ||
| 7516 | 10년 전 | 1269 | ||
| 7515 | 10년 전 | 1390 | ||
| 7514 |
|
10년 전 | 4479 | |
| 7513 |
멋진남자임
|
10년 전 | 1127 | |
| 7512 |
다빈치코드777
|
10년 전 | 861 | |
| 7511 |
|
10년 전 | 3381 | |
| 7510 | 10년 전 | 1352 | ||
| 7509 | 10년 전 | 1122 | ||
| 7508 | 10년 전 | 705 | ||
| 7507 |
senseme
|
10년 전 | 724 | |
| 7506 |
멋진남자임
|
10년 전 | 1622 | |
| 7505 | 10년 전 | 3979 | ||
| 7504 | 10년 전 | 2135 | ||
| 7503 | 10년 전 | 987 | ||
| 7502 | 10년 전 | 512 | ||
| 7501 | 10년 전 | 1430 | ||
| 7500 | 10년 전 | 1475 | ||
| 7499 | 10년 전 | 3358 | ||
| 7498 | 10년 전 | 1181 | ||
| 7497 |
dethos79
|
10년 전 | 2953 | |
| 7496 | 10년 전 | 2124 | ||
| 7495 | 10년 전 | 830 | ||
| 7494 |
CHAVO
|
10년 전 | 1112 | |
| 7493 | 10년 전 | 2630 | ||
| 7492 | 10년 전 | 1231 | ||
| 7491 | 10년 전 | 1440 | ||
| 7490 | 10년 전 | 2320 | ||
| 7489 | 10년 전 | 2112 | ||
| 7488 |
toptopon
|
10년 전 | 880 | |
| 7487 |
|
10년 전 | 1021 | |
| 7486 | 10년 전 | 3337 | ||
| 7485 | 10년 전 | 1299 | ||
| 7484 | 10년 전 | 1358 | ||
| 7483 | 10년 전 | 1015 | ||
| 7482 | 10년 전 | 642 | ||
| 7481 | 10년 전 | 852 | ||
| 7480 | 10년 전 | 1212 | ||
| 7479 | 10년 전 | 2592 | ||
| 7478 | 10년 전 | 1143 | ||
| 7477 |
멋진남자임
|
10년 전 | 1494 | |
| 7476 |
zeppeto
|
10년 전 | 1132 | |
| 7475 |
200점아빠
|
10년 전 | 906 | |
| 7474 | 10년 전 | 3996 | ||
| 7473 | 10년 전 | 976 | ||
| 7472 |
나르시스1
|
10년 전 | 1222 | |
| 7471 | 10년 전 | 867 | ||
| 7470 | 10년 전 | 1263 | ||
| 7469 |
플라이SINJI
|
10년 전 | 946 | |
| 7468 |
|
10년 전 | 529 | |
| 7467 |
|
10년 전 | 638 | |
| 7466 | 10년 전 | 1101 | ||
| 7465 | 10년 전 | 1167 | ||
| 7464 |
|
10년 전 | 1173 | |
| 7463 | 10년 전 | 1225 | ||
| 7462 |
진짜별사탕
|
10년 전 | 841 | |
| 7461 | 10년 전 | 931 | ||
| 7460 | 10년 전 | 3710 | ||
| 7459 |
멋진남자임
|
10년 전 | 1543 | |
| 7458 |
멋진남자임
|
10년 전 | 475 | |
| 7457 | 10년 전 | 909 | ||
| 7456 | 10년 전 | 748 | ||
| 7455 | 10년 전 | 2131 | ||
| 7454 | 10년 전 | 622 | ||
| 7453 | 10년 전 | 810 | ||
| 7452 |
중국어사이트제작
|
10년 전 | 501 | |
| 7451 | 10년 전 | 895 | ||
| 7450 | 10년 전 | 620 | ||
| 7449 |
울라라라우
|
10년 전 | 932 | |
| 7448 | 10년 전 | 1625 | ||
| 7447 |
멋진남자임
|
10년 전 | 489 | |
| 7446 | 10년 전 | 533 | ||
| 7445 |
네이비칼라
|
10년 전 | 1651 | |
| 7444 |
senseme
|
10년 전 | 1391 | |
| 7443 | 10년 전 | 1325 | ||
| 7442 | 10년 전 | 712 | ||
| 7441 |
멋진남자임
|
10년 전 | 1423 | |
| 7440 | 10년 전 | 881 | ||
| 7439 |
|
10년 전 | 739 | |
| 7438 |
|
10년 전 | 916 | |
| 7437 |
basement
|
10년 전 | 1023 | |
| 7436 |
잘살아보자
|
10년 전 | 1113 | |
| 7435 | 10년 전 | 1063 | ||
| 7434 | 10년 전 | 3781 | ||
| 7433 |
|
10년 전 | 2719 | |
| 7432 |
alexkim
|
10년 전 | 850 | |
| 7431 |
이웃집초보
|
10년 전 | 1296 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기