유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8130 | 9년 전 | 461 | ||
| 8129 |
|
9년 전 | 626 | |
| 8128 | 9년 전 | 498 | ||
| 8127 |
|
9년 전 | 559 | |
| 8126 | 9년 전 | 491 | ||
| 8125 | 9년 전 | 749 | ||
| 8124 |
|
9년 전 | 480 | |
| 8123 | 9년 전 | 465 | ||
| 8122 | 9년 전 | 402 | ||
| 8121 | 9년 전 | 510 | ||
| 8120 | 9년 전 | 443 | ||
| 8119 | 9년 전 | 523 | ||
| 8118 |
|
9년 전 | 607 | |
| 8117 |
|
9년 전 | 381 | |
| 8116 |
PASKRAN
|
9년 전 | 450 | |
| 8115 | 9년 전 | 442 | ||
| 8114 |
kiplayer
|
9년 전 | 584 | |
| 8113 | 9년 전 | 416 | ||
| 8112 |
|
9년 전 | 532 | |
| 8111 | 9년 전 | 382 | ||
| 8110 | 9년 전 | 424 | ||
| 8109 | 9년 전 | 334 | ||
| 8108 |
|
9년 전 | 524 | |
| 8107 |
|
9년 전 | 414 | |
| 8106 |
|
9년 전 | 409 | |
| 8105 | 9년 전 | 456 | ||
| 8104 |
|
9년 전 | 406 | |
| 8103 |
|
9년 전 | 408 | |
| 8102 |
|
9년 전 | 361 | |
| 8101 |
snshero
|
9년 전 | 750 | |
| 8100 | 9년 전 | 817 | ||
| 8099 | 9년 전 | 781 | ||
| 8098 | 9년 전 | 692 | ||
| 8097 | 9년 전 | 481 | ||
| 8096 | 9년 전 | 683 | ||
| 8095 | 9년 전 | 799 | ||
| 8094 | 9년 전 | 475 | ||
| 8093 | 9년 전 | 774 | ||
| 8092 | 9년 전 | 736 | ||
| 8091 | 9년 전 | 1126 | ||
| 8090 | 9년 전 | 736 | ||
| 8089 | 9년 전 | 942 | ||
| 8088 | 9년 전 | 615 | ||
| 8087 | 9년 전 | 722 | ||
| 8086 | 9년 전 | 494 | ||
| 8085 | 9년 전 | 457 | ||
| 8084 | 9년 전 | 600 | ||
| 8083 | 9년 전 | 569 | ||
| 8082 | 9년 전 | 735 | ||
| 8081 | 9년 전 | 447 | ||
| 8080 | 9년 전 | 532 | ||
| 8079 | 9년 전 | 489 | ||
| 8078 | 9년 전 | 420 | ||
| 8077 | 9년 전 | 495 | ||
| 8076 | 9년 전 | 384 | ||
| 8075 | 9년 전 | 398 | ||
| 8074 | 9년 전 | 366 | ||
| 8073 | 9년 전 | 416 | ||
| 8072 | 9년 전 | 405 | ||
| 8071 |
o1o111
|
9년 전 | 856 | |
| 8070 | 9년 전 | 374 | ||
| 8069 | 9년 전 | 303 | ||
| 8068 | 9년 전 | 567 | ||
| 8067 | 9년 전 | 374 | ||
| 8066 | 9년 전 | 402 | ||
| 8065 | 9년 전 | 353 | ||
| 8064 | 9년 전 | 333 | ||
| 8063 | 9년 전 | 294 | ||
| 8062 | 9년 전 | 285 | ||
| 8061 | 9년 전 | 331 | ||
| 8060 | 9년 전 | 352 | ||
| 8059 | 9년 전 | 285 | ||
| 8058 | 9년 전 | 225 | ||
| 8057 | 9년 전 | 350 | ||
| 8056 | 9년 전 | 280 | ||
| 8055 | 9년 전 | 312 | ||
| 8054 | 9년 전 | 330 | ||
| 8053 | 9년 전 | 371 | ||
| 8052 | 9년 전 | 258 | ||
| 8051 | 9년 전 | 303 | ||
| 8050 | 9년 전 | 367 | ||
| 8049 | 9년 전 | 288 | ||
| 8048 | 9년 전 | 401 | ||
| 8047 | 9년 전 | 336 | ||
| 8046 | 9년 전 | 263 | ||
| 8045 | 9년 전 | 237 | ||
| 8044 | 9년 전 | 300 | ||
| 8043 | 9년 전 | 241 | ||
| 8042 | 9년 전 | 268 | ||
| 8041 | 9년 전 | 306 | ||
| 8040 | 9년 전 | 238 | ||
| 8039 | 9년 전 | 299 | ||
| 8038 | 9년 전 | 251 | ||
| 8037 | 9년 전 | 363 | ||
| 8036 | 9년 전 | 450 | ||
| 8035 | 9년 전 | 402 | ||
| 8034 | 9년 전 | 364 | ||
| 8033 | 9년 전 | 319 | ||
| 8032 | 9년 전 | 370 | ||
| 8031 | 9년 전 | 309 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기