유용한 함수 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년 전 | 465 | ||
| 7929 | 9년 전 | 405 | ||
| 7928 | 9년 전 | 475 | ||
| 7927 | 9년 전 | 381 | ||
| 7926 | 9년 전 | 690 | ||
| 7925 | 9년 전 | 413 | ||
| 7924 | 9년 전 | 397 | ||
| 7923 | 9년 전 | 381 | ||
| 7922 | 9년 전 | 416 | ||
| 7921 | 9년 전 | 434 | ||
| 7920 | 9년 전 | 344 | ||
| 7919 | 9년 전 | 354 | ||
| 7918 | 9년 전 | 508 | ||
| 7917 | 9년 전 | 360 | ||
| 7916 | 9년 전 | 447 | ||
| 7915 | 9년 전 | 457 | ||
| 7914 | 9년 전 | 469 | ||
| 7913 | 9년 전 | 646 | ||
| 7912 | 9년 전 | 474 | ||
| 7911 | 9년 전 | 399 | ||
| 7910 | 9년 전 | 455 | ||
| 7909 | 9년 전 | 561 | ||
| 7908 | 9년 전 | 505 | ||
| 7907 | 9년 전 | 431 | ||
| 7906 | 9년 전 | 458 | ||
| 7905 | 9년 전 | 432 | ||
| 7904 | 9년 전 | 415 | ||
| 7903 | 9년 전 | 421 | ||
| 7902 | 9년 전 | 595 | ||
| 7901 |
|
9년 전 | 768 | |
| 7900 | 9년 전 | 661 | ||
| 7899 | 9년 전 | 438 | ||
| 7898 | 9년 전 | 438 | ||
| 7897 | 9년 전 | 397 | ||
| 7896 | 9년 전 | 414 | ||
| 7895 | 9년 전 | 533 | ||
| 7894 | 9년 전 | 437 | ||
| 7893 | 9년 전 | 420 | ||
| 7892 | 9년 전 | 456 | ||
| 7891 | 9년 전 | 809 | ||
| 7890 | 9년 전 | 1232 | ||
| 7889 | 9년 전 | 767 | ||
| 7888 |
limsy1987
|
9년 전 | 578 | |
| 7887 | 9년 전 | 631 | ||
| 7886 | 9년 전 | 513 | ||
| 7885 | 9년 전 | 481 | ||
| 7884 | 9년 전 | 466 | ||
| 7883 | 9년 전 | 486 | ||
| 7882 | 9년 전 | 544 | ||
| 7881 | 9년 전 | 520 | ||
| 7880 | 9년 전 | 631 | ||
| 7879 | 9년 전 | 519 | ||
| 7878 | 9년 전 | 1291 | ||
| 7877 | 9년 전 | 814 | ||
| 7876 | 9년 전 | 561 | ||
| 7875 | 9년 전 | 629 | ||
| 7874 |
|
9년 전 | 830 | |
| 7873 | 9년 전 | 553 | ||
| 7872 | 9년 전 | 728 | ||
| 7871 | 9년 전 | 542 | ||
| 7870 | 9년 전 | 656 | ||
| 7869 | 9년 전 | 473 | ||
| 7868 | 9년 전 | 518 | ||
| 7867 | 9년 전 | 519 | ||
| 7866 | 9년 전 | 580 | ||
| 7865 | 9년 전 | 521 | ||
| 7864 | 9년 전 | 581 | ||
| 7863 | 9년 전 | 575 | ||
| 7862 | 9년 전 | 531 | ||
| 7861 | 9년 전 | 702 | ||
| 7860 | 9년 전 | 677 | ||
| 7859 | 9년 전 | 450 | ||
| 7858 | 9년 전 | 772 | ||
| 7857 | 9년 전 | 1154 | ||
| 7856 | 9년 전 | 577 | ||
| 7855 | 9년 전 | 812 | ||
| 7854 | 9년 전 | 754 | ||
| 7853 | 9년 전 | 652 | ||
| 7852 | 9년 전 | 567 | ||
| 7851 | 9년 전 | 582 | ||
| 7850 | 9년 전 | 646 | ||
| 7849 | 9년 전 | 410 | ||
| 7848 | 9년 전 | 470 | ||
| 7847 | 10년 전 | 728 | ||
| 7846 | 10년 전 | 502 | ||
| 7845 | 10년 전 | 484 | ||
| 7844 | 10년 전 | 440 | ||
| 7843 | 10년 전 | 485 | ||
| 7842 | 10년 전 | 465 | ||
| 7841 | 10년 전 | 431 | ||
| 7840 | 10년 전 | 447 | ||
| 7839 | 10년 전 | 510 | ||
| 7838 | 10년 전 | 555 | ||
| 7837 | 10년 전 | 386 | ||
| 7836 | 10년 전 | 445 | ||
| 7835 | 10년 전 | 524 | ||
| 7834 |
|
10년 전 | 1229 | |
| 7833 | 10년 전 | 496 | ||
| 7832 | 10년 전 | 466 | ||
| 7831 | 10년 전 | 628 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기