유용한 함수 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년 전 | 365 | ||
| 7929 | 9년 전 | 260 | ||
| 7928 | 9년 전 | 368 | ||
| 7927 | 9년 전 | 295 | ||
| 7926 | 9년 전 | 641 | ||
| 7925 | 9년 전 | 311 | ||
| 7924 | 9년 전 | 282 | ||
| 7923 | 9년 전 | 320 | ||
| 7922 | 9년 전 | 339 | ||
| 7921 | 9년 전 | 373 | ||
| 7920 | 9년 전 | 293 | ||
| 7919 | 9년 전 | 310 | ||
| 7918 | 9년 전 | 474 | ||
| 7917 | 9년 전 | 313 | ||
| 7916 | 9년 전 | 378 | ||
| 7915 | 9년 전 | 364 | ||
| 7914 | 9년 전 | 379 | ||
| 7913 | 9년 전 | 503 | ||
| 7912 | 9년 전 | 374 | ||
| 7911 | 9년 전 | 330 | ||
| 7910 | 9년 전 | 359 | ||
| 7909 | 9년 전 | 470 | ||
| 7908 | 9년 전 | 367 | ||
| 7907 | 9년 전 | 318 | ||
| 7906 | 9년 전 | 335 | ||
| 7905 | 9년 전 | 327 | ||
| 7904 | 9년 전 | 295 | ||
| 7903 | 9년 전 | 292 | ||
| 7902 | 9년 전 | 512 | ||
| 7901 |
|
9년 전 | 716 | |
| 7900 | 9년 전 | 536 | ||
| 7899 | 9년 전 | 344 | ||
| 7898 | 9년 전 | 338 | ||
| 7897 | 9년 전 | 295 | ||
| 7896 | 9년 전 | 314 | ||
| 7895 | 9년 전 | 423 | ||
| 7894 | 9년 전 | 346 | ||
| 7893 | 9년 전 | 276 | ||
| 7892 | 9년 전 | 322 | ||
| 7891 | 9년 전 | 708 | ||
| 7890 | 9년 전 | 1172 | ||
| 7889 | 9년 전 | 729 | ||
| 7888 |
limsy1987
|
9년 전 | 522 | |
| 7887 | 9년 전 | 499 | ||
| 7886 | 9년 전 | 385 | ||
| 7885 | 9년 전 | 358 | ||
| 7884 | 9년 전 | 366 | ||
| 7883 | 9년 전 | 345 | ||
| 7882 | 9년 전 | 359 | ||
| 7881 | 9년 전 | 387 | ||
| 7880 | 9년 전 | 513 | ||
| 7879 | 9년 전 | 411 | ||
| 7878 | 9년 전 | 1154 | ||
| 7877 | 9년 전 | 705 | ||
| 7876 | 9년 전 | 432 | ||
| 7875 | 9년 전 | 505 | ||
| 7874 |
|
9년 전 | 778 | |
| 7873 | 9년 전 | 481 | ||
| 7872 | 9년 전 | 612 | ||
| 7871 | 9년 전 | 444 | ||
| 7870 | 9년 전 | 566 | ||
| 7869 | 9년 전 | 384 | ||
| 7868 | 9년 전 | 379 | ||
| 7867 | 9년 전 | 379 | ||
| 7866 | 9년 전 | 437 | ||
| 7865 | 9년 전 | 395 | ||
| 7864 | 9년 전 | 457 | ||
| 7863 | 9년 전 | 450 | ||
| 7862 | 9년 전 | 426 | ||
| 7861 | 9년 전 | 588 | ||
| 7860 | 9년 전 | 593 | ||
| 7859 | 9년 전 | 362 | ||
| 7858 | 9년 전 | 670 | ||
| 7857 | 9년 전 | 1020 | ||
| 7856 | 9년 전 | 483 | ||
| 7855 | 9년 전 | 696 | ||
| 7854 | 9년 전 | 698 | ||
| 7853 | 9년 전 | 531 | ||
| 7852 | 9년 전 | 470 | ||
| 7851 | 9년 전 | 445 | ||
| 7850 | 9년 전 | 541 | ||
| 7849 | 9년 전 | 312 | ||
| 7848 | 9년 전 | 364 | ||
| 7847 | 9년 전 | 586 | ||
| 7846 | 9년 전 | 403 | ||
| 7845 | 9년 전 | 367 | ||
| 7844 | 9년 전 | 364 | ||
| 7843 | 9년 전 | 368 | ||
| 7842 | 9년 전 | 366 | ||
| 7841 | 9년 전 | 353 | ||
| 7840 | 9년 전 | 359 | ||
| 7839 | 9년 전 | 392 | ||
| 7838 | 9년 전 | 477 | ||
| 7837 | 9년 전 | 318 | ||
| 7836 | 9년 전 | 356 | ||
| 7835 | 9년 전 | 437 | ||
| 7834 |
|
9년 전 | 1168 | |
| 7833 | 9년 전 | 373 | ||
| 7832 | 9년 전 | 365 | ||
| 7831 | 9년 전 | 501 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기