유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 730 |
|
19년 전 | 2727 | |
| 729 |
그레이스웹
|
19년 전 | 3519 | |
| 728 |
|
19년 전 | 2696 | |
| 727 |
|
19년 전 | 2533 | |
| 726 |
|
19년 전 | 2351 | |
| 725 |
|
19년 전 | 2256 | |
| 724 |
|
19년 전 | 2137 | |
| 723 |
|
19년 전 | 4260 | |
| 722 |
|
19년 전 | 2680 | |
| 721 |
|
19년 전 | 2169 | |
| 720 |
|
19년 전 | 2274 | |
| 719 |
|
19년 전 | 2856 | |
| 718 |
|
19년 전 | 1964 | |
| 717 |
|
19년 전 | 3198 | |
| 716 |
|
19년 전 | 2757 | |
| 715 |
|
19년 전 | 2275 | |
| 714 |
|
19년 전 | 1827 | |
| 713 |
|
19년 전 | 2243 | |
| 712 |
|
19년 전 | 2015 | |
| 711 |
|
19년 전 | 1911 | |
| 710 |
|
19년 전 | 2476 | |
| 709 |
|
19년 전 | 2337 | |
| 708 |
|
19년 전 | 3897 | |
| 707 |
|
19년 전 | 3742 | |
| 706 |
|
19년 전 | 2240 | |
| 705 |
|
19년 전 | 3317 | |
| 704 |
|
19년 전 | 1984 | |
| 703 |
|
19년 전 | 2389 | |
| 702 |
|
19년 전 | 2283 | |
| 701 |
홀로남은자
|
19년 전 | 2349 | |
| 700 |
홀로남은자
|
19년 전 | 2187 | |
| 699 | 19년 전 | 2817 | ||
| 698 |
|
19년 전 | 2843 | |
| 697 |
|
19년 전 | 3179 | |
| 696 |
|
19년 전 | 3049 | |
| 695 |
|
19년 전 | 2715 | |
| 694 |
|
19년 전 | 2948 | |
| 693 |
|
19년 전 | 3195 | |
| 692 |
|
19년 전 | 2883 | |
| 691 |
|
19년 전 | 2629 | |
| 690 | 19년 전 | 2931 | ||
| 689 | 19년 전 | 4516 | ||
| 688 | 19년 전 | 2441 | ||
| 687 | 19년 전 | 2454 | ||
| 686 | 19년 전 | 3486 | ||
| 685 | 19년 전 | 3173 | ||
| 684 | 19년 전 | 2833 | ||
| 683 | 19년 전 | 2065 | ||
| 682 | 19년 전 | 1777 | ||
| 681 | 19년 전 | 2878 | ||
| 680 | 19년 전 | 1730 | ||
| 679 | 19년 전 | 2313 | ||
| 678 | 19년 전 | 3965 | ||
| 677 | 19년 전 | 3743 | ||
| 676 | 19년 전 | 3472 | ||
| 675 | 19년 전 | 3413 | ||
| 674 |
|
19년 전 | 1570 | |
| 673 |
|
19년 전 | 1939 | |
| 672 |
|
19년 전 | 1866 | |
| 671 | 19년 전 | 2531 | ||
| 670 | 19년 전 | 4636 | ||
| 669 |
|
19년 전 | 2770 | |
| 668 |
|
19년 전 | 1929 | |
| 667 |
|
19년 전 | 1941 | |
| 666 |
|
19년 전 | 1846 | |
| 665 |
|
19년 전 | 2582 | |
| 664 |
|
19년 전 | 8181 | |
| 663 |
|
19년 전 | 2733 | |
| 662 |
|
19년 전 | 2792 | |
| 661 |
|
19년 전 | 3045 | |
| 660 |
|
19년 전 | 2216 | |
| 659 |
|
19년 전 | 2232 | |
| 658 |
|
19년 전 | 2179 | |
| 657 |
|
19년 전 | 2085 | |
| 656 |
|
19년 전 | 2276 | |
| 655 |
|
19년 전 | 2539 | |
| 654 |
|
19년 전 | 3084 | |
| 653 | 19년 전 | 2333 | ||
| 652 | 19년 전 | 1917 | ||
| 651 |
|
19년 전 | 2850 | |
| 650 | 19년 전 | 5015 | ||
| 649 | 19년 전 | 3510 | ||
| 648 | 19년 전 | 3465 | ||
| 647 | 19년 전 | 2977 | ||
| 646 | 19년 전 | 2409 | ||
| 645 | 19년 전 | 1501 | ||
| 644 | 19년 전 | 3142 | ||
| 643 | 19년 전 | 2023 | ||
| 642 |
|
19년 전 | 5396 | |
| 641 | 19년 전 | 2437 | ||
| 640 | 19년 전 | 3437 | ||
| 639 | 19년 전 | 2871 | ||
| 638 | 19년 전 | 1769 | ||
| 637 | 19년 전 | 3880 | ||
| 636 | 19년 전 | 2431 | ||
| 635 | 19년 전 | 2320 | ||
| 634 |
|
19년 전 | 3026 | |
| 633 |
|
19년 전 | 3307 | |
| 632 | 19년 전 | 2509 | ||
| 631 | 19년 전 | 2269 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기