유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 401 | ||
| 7929 | 9년 전 | 320 | ||
| 7928 | 9년 전 | 411 | ||
| 7927 | 9년 전 | 329 | ||
| 7926 | 9년 전 | 660 | ||
| 7925 | 9년 전 | 344 | ||
| 7924 | 9년 전 | 331 | ||
| 7923 | 9년 전 | 337 | ||
| 7922 | 9년 전 | 369 | ||
| 7921 | 9년 전 | 395 | ||
| 7920 | 9년 전 | 316 | ||
| 7919 | 9년 전 | 330 | ||
| 7918 | 9년 전 | 486 | ||
| 7917 | 9년 전 | 324 | ||
| 7916 | 9년 전 | 399 | ||
| 7915 | 9년 전 | 399 | ||
| 7914 | 9년 전 | 407 | ||
| 7913 | 9년 전 | 566 | ||
| 7912 | 9년 전 | 412 | ||
| 7911 | 9년 전 | 360 | ||
| 7910 | 9년 전 | 402 | ||
| 7909 | 9년 전 | 501 | ||
| 7908 | 9년 전 | 416 | ||
| 7907 | 9년 전 | 362 | ||
| 7906 | 9년 전 | 383 | ||
| 7905 | 9년 전 | 359 | ||
| 7904 | 9년 전 | 349 | ||
| 7903 | 9년 전 | 341 | ||
| 7902 | 9년 전 | 555 | ||
| 7901 |
|
9년 전 | 727 | |
| 7900 | 9년 전 | 578 | ||
| 7899 | 9년 전 | 373 | ||
| 7898 | 9년 전 | 378 | ||
| 7897 | 9년 전 | 331 | ||
| 7896 | 9년 전 | 353 | ||
| 7895 | 9년 전 | 461 | ||
| 7894 | 9년 전 | 382 | ||
| 7893 | 9년 전 | 327 | ||
| 7892 | 9년 전 | 379 | ||
| 7891 | 9년 전 | 754 | ||
| 7890 | 9년 전 | 1188 | ||
| 7889 | 9년 전 | 742 | ||
| 7888 |
limsy1987
|
9년 전 | 542 | |
| 7887 | 9년 전 | 540 | ||
| 7886 | 9년 전 | 423 | ||
| 7885 | 9년 전 | 397 | ||
| 7884 | 9년 전 | 401 | ||
| 7883 | 9년 전 | 396 | ||
| 7882 | 9년 전 | 440 | ||
| 7881 | 9년 전 | 431 | ||
| 7880 | 9년 전 | 552 | ||
| 7879 | 9년 전 | 453 | ||
| 7878 | 9년 전 | 1208 | ||
| 7877 | 9년 전 | 745 | ||
| 7876 | 9년 전 | 473 | ||
| 7875 | 9년 전 | 553 | ||
| 7874 |
|
9년 전 | 800 | |
| 7873 | 9년 전 | 513 | ||
| 7872 | 9년 전 | 667 | ||
| 7871 | 9년 전 | 473 | ||
| 7870 | 9년 전 | 599 | ||
| 7869 | 9년 전 | 428 | ||
| 7868 | 9년 전 | 433 | ||
| 7867 | 9년 전 | 415 | ||
| 7866 | 9년 전 | 479 | ||
| 7865 | 9년 전 | 442 | ||
| 7864 | 9년 전 | 506 | ||
| 7863 | 9년 전 | 502 | ||
| 7862 | 9년 전 | 461 | ||
| 7861 | 9년 전 | 622 | ||
| 7860 | 9년 전 | 613 | ||
| 7859 | 9년 전 | 403 | ||
| 7858 | 9년 전 | 694 | ||
| 7857 | 9년 전 | 1067 | ||
| 7856 | 9년 전 | 513 | ||
| 7855 | 9년 전 | 741 | ||
| 7854 | 9년 전 | 726 | ||
| 7853 | 9년 전 | 573 | ||
| 7852 | 9년 전 | 501 | ||
| 7851 | 9년 전 | 497 | ||
| 7850 | 9년 전 | 579 | ||
| 7849 | 9년 전 | 354 | ||
| 7848 | 9년 전 | 406 | ||
| 7847 | 9년 전 | 644 | ||
| 7846 | 9년 전 | 449 | ||
| 7845 | 9년 전 | 408 | ||
| 7844 | 9년 전 | 387 | ||
| 7843 | 9년 전 | 407 | ||
| 7842 | 9년 전 | 390 | ||
| 7841 | 9년 전 | 377 | ||
| 7840 | 9년 전 | 397 | ||
| 7839 | 9년 전 | 428 | ||
| 7838 | 9년 전 | 511 | ||
| 7837 | 9년 전 | 348 | ||
| 7836 | 9년 전 | 393 | ||
| 7835 | 9년 전 | 466 | ||
| 7834 |
|
9년 전 | 1187 | |
| 7833 | 9년 전 | 418 | ||
| 7832 | 9년 전 | 407 | ||
| 7831 | 9년 전 | 553 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기