유용한 함수 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년 전 | 394 | ||
| 7929 | 9년 전 | 302 | ||
| 7928 | 9년 전 | 400 | ||
| 7927 | 9년 전 | 321 | ||
| 7926 | 9년 전 | 652 | ||
| 7925 | 9년 전 | 337 | ||
| 7924 | 9년 전 | 317 | ||
| 7923 | 9년 전 | 332 | ||
| 7922 | 9년 전 | 352 | ||
| 7921 | 9년 전 | 389 | ||
| 7920 | 9년 전 | 307 | ||
| 7919 | 9년 전 | 327 | ||
| 7918 | 9년 전 | 481 | ||
| 7917 | 9년 전 | 323 | ||
| 7916 | 9년 전 | 394 | ||
| 7915 | 9년 전 | 392 | ||
| 7914 | 9년 전 | 397 | ||
| 7913 | 9년 전 | 547 | ||
| 7912 | 9년 전 | 398 | ||
| 7911 | 9년 전 | 351 | ||
| 7910 | 9년 전 | 387 | ||
| 7909 | 9년 전 | 491 | ||
| 7908 | 9년 전 | 404 | ||
| 7907 | 9년 전 | 347 | ||
| 7906 | 9년 전 | 368 | ||
| 7905 | 9년 전 | 348 | ||
| 7904 | 9년 전 | 335 | ||
| 7903 | 9년 전 | 332 | ||
| 7902 | 9년 전 | 539 | ||
| 7901 |
|
9년 전 | 723 | |
| 7900 | 9년 전 | 557 | ||
| 7899 | 9년 전 | 362 | ||
| 7898 | 9년 전 | 368 | ||
| 7897 | 9년 전 | 324 | ||
| 7896 | 9년 전 | 345 | ||
| 7895 | 9년 전 | 450 | ||
| 7894 | 9년 전 | 368 | ||
| 7893 | 9년 전 | 314 | ||
| 7892 | 9년 전 | 362 | ||
| 7891 | 9년 전 | 746 | ||
| 7890 | 9년 전 | 1184 | ||
| 7889 | 9년 전 | 739 | ||
| 7888 |
limsy1987
|
9년 전 | 534 | |
| 7887 | 9년 전 | 526 | ||
| 7886 | 9년 전 | 417 | ||
| 7885 | 9년 전 | 386 | ||
| 7884 | 9년 전 | 392 | ||
| 7883 | 9년 전 | 382 | ||
| 7882 | 9년 전 | 415 | ||
| 7881 | 9년 전 | 422 | ||
| 7880 | 9년 전 | 545 | ||
| 7879 | 9년 전 | 441 | ||
| 7878 | 9년 전 | 1193 | ||
| 7877 | 9년 전 | 731 | ||
| 7876 | 9년 전 | 466 | ||
| 7875 | 9년 전 | 539 | ||
| 7874 |
|
9년 전 | 796 | |
| 7873 | 9년 전 | 503 | ||
| 7872 | 9년 전 | 653 | ||
| 7871 | 9년 전 | 465 | ||
| 7870 | 9년 전 | 595 | ||
| 7869 | 9년 전 | 412 | ||
| 7868 | 9년 전 | 421 | ||
| 7867 | 9년 전 | 402 | ||
| 7866 | 9년 전 | 471 | ||
| 7865 | 9년 전 | 428 | ||
| 7864 | 9년 전 | 497 | ||
| 7863 | 9년 전 | 495 | ||
| 7862 | 9년 전 | 453 | ||
| 7861 | 9년 전 | 612 | ||
| 7860 | 9년 전 | 606 | ||
| 7859 | 9년 전 | 392 | ||
| 7858 | 9년 전 | 688 | ||
| 7857 | 9년 전 | 1051 | ||
| 7856 | 9년 전 | 498 | ||
| 7855 | 9년 전 | 725 | ||
| 7854 | 9년 전 | 713 | ||
| 7853 | 9년 전 | 567 | ||
| 7852 | 9년 전 | 490 | ||
| 7851 | 9년 전 | 484 | ||
| 7850 | 9년 전 | 570 | ||
| 7849 | 9년 전 | 341 | ||
| 7848 | 9년 전 | 395 | ||
| 7847 | 9년 전 | 628 | ||
| 7846 | 9년 전 | 439 | ||
| 7845 | 9년 전 | 394 | ||
| 7844 | 9년 전 | 378 | ||
| 7843 | 9년 전 | 396 | ||
| 7842 | 9년 전 | 382 | ||
| 7841 | 9년 전 | 369 | ||
| 7840 | 9년 전 | 382 | ||
| 7839 | 9년 전 | 418 | ||
| 7838 | 9년 전 | 500 | ||
| 7837 | 9년 전 | 340 | ||
| 7836 | 9년 전 | 380 | ||
| 7835 | 9년 전 | 456 | ||
| 7834 |
|
9년 전 | 1181 | |
| 7833 | 9년 전 | 400 | ||
| 7832 | 9년 전 | 396 | ||
| 7831 | 9년 전 | 534 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기