유용한 함수 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년 전 | 392 | ||
| 7929 | 9년 전 | 299 | ||
| 7928 | 9년 전 | 395 | ||
| 7927 | 9년 전 | 318 | ||
| 7926 | 9년 전 | 652 | ||
| 7925 | 9년 전 | 333 | ||
| 7924 | 9년 전 | 315 | ||
| 7923 | 9년 전 | 330 | ||
| 7922 | 9년 전 | 351 | ||
| 7921 | 9년 전 | 384 | ||
| 7920 | 9년 전 | 305 | ||
| 7919 | 9년 전 | 324 | ||
| 7918 | 9년 전 | 480 | ||
| 7917 | 9년 전 | 323 | ||
| 7916 | 9년 전 | 391 | ||
| 7915 | 9년 전 | 389 | ||
| 7914 | 9년 전 | 396 | ||
| 7913 | 9년 전 | 542 | ||
| 7912 | 9년 전 | 397 | ||
| 7911 | 9년 전 | 346 | ||
| 7910 | 9년 전 | 384 | ||
| 7909 | 9년 전 | 488 | ||
| 7908 | 9년 전 | 400 | ||
| 7907 | 9년 전 | 344 | ||
| 7906 | 9년 전 | 364 | ||
| 7905 | 9년 전 | 344 | ||
| 7904 | 9년 전 | 332 | ||
| 7903 | 9년 전 | 330 | ||
| 7902 | 9년 전 | 538 | ||
| 7901 |
|
9년 전 | 722 | |
| 7900 | 9년 전 | 556 | ||
| 7899 | 9년 전 | 360 | ||
| 7898 | 9년 전 | 363 | ||
| 7897 | 9년 전 | 320 | ||
| 7896 | 9년 전 | 341 | ||
| 7895 | 9년 전 | 448 | ||
| 7894 | 9년 전 | 366 | ||
| 7893 | 9년 전 | 306 | ||
| 7892 | 9년 전 | 360 | ||
| 7891 | 9년 전 | 739 | ||
| 7890 | 9년 전 | 1183 | ||
| 7889 | 9년 전 | 739 | ||
| 7888 |
limsy1987
|
9년 전 | 532 | |
| 7887 | 9년 전 | 523 | ||
| 7886 | 9년 전 | 415 | ||
| 7885 | 9년 전 | 386 | ||
| 7884 | 9년 전 | 389 | ||
| 7883 | 9년 전 | 377 | ||
| 7882 | 9년 전 | 413 | ||
| 7881 | 9년 전 | 417 | ||
| 7880 | 9년 전 | 541 | ||
| 7879 | 9년 전 | 439 | ||
| 7878 | 9년 전 | 1189 | ||
| 7877 | 9년 전 | 729 | ||
| 7876 | 9년 전 | 463 | ||
| 7875 | 9년 전 | 536 | ||
| 7874 |
|
9년 전 | 792 | |
| 7873 | 9년 전 | 498 | ||
| 7872 | 9년 전 | 650 | ||
| 7871 | 9년 전 | 462 | ||
| 7870 | 9년 전 | 593 | ||
| 7869 | 9년 전 | 406 | ||
| 7868 | 9년 전 | 418 | ||
| 7867 | 9년 전 | 401 | ||
| 7866 | 9년 전 | 469 | ||
| 7865 | 9년 전 | 426 | ||
| 7864 | 9년 전 | 496 | ||
| 7863 | 9년 전 | 492 | ||
| 7862 | 9년 전 | 448 | ||
| 7861 | 9년 전 | 610 | ||
| 7860 | 9년 전 | 606 | ||
| 7859 | 9년 전 | 388 | ||
| 7858 | 9년 전 | 687 | ||
| 7857 | 9년 전 | 1045 | ||
| 7856 | 9년 전 | 496 | ||
| 7855 | 9년 전 | 725 | ||
| 7854 | 9년 전 | 710 | ||
| 7853 | 9년 전 | 565 | ||
| 7852 | 9년 전 | 485 | ||
| 7851 | 9년 전 | 482 | ||
| 7850 | 9년 전 | 569 | ||
| 7849 | 9년 전 | 338 | ||
| 7848 | 9년 전 | 391 | ||
| 7847 | 9년 전 | 627 | ||
| 7846 | 9년 전 | 433 | ||
| 7845 | 9년 전 | 392 | ||
| 7844 | 9년 전 | 377 | ||
| 7843 | 9년 전 | 392 | ||
| 7842 | 9년 전 | 381 | ||
| 7841 | 9년 전 | 367 | ||
| 7840 | 9년 전 | 380 | ||
| 7839 | 9년 전 | 417 | ||
| 7838 | 9년 전 | 498 | ||
| 7837 | 9년 전 | 340 | ||
| 7836 | 9년 전 | 378 | ||
| 7835 | 9년 전 | 455 | ||
| 7834 |
|
9년 전 | 1177 | |
| 7833 | 9년 전 | 397 | ||
| 7832 | 9년 전 | 394 | ||
| 7831 | 9년 전 | 530 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기