유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 830 |
|
19년 전 | 2192 | |
| 829 |
|
19년 전 | 1934 | |
| 828 |
|
19년 전 | 1818 | |
| 827 |
|
19년 전 | 1657 | |
| 826 |
|
19년 전 | 1854 | |
| 825 |
|
19년 전 | 1901 | |
| 824 |
|
19년 전 | 1959 | |
| 823 |
|
19년 전 | 2695 | |
| 822 |
|
19년 전 | 5329 | |
| 821 |
|
19년 전 | 1728 | |
| 820 |
|
19년 전 | 1582 | |
| 819 |
|
19년 전 | 1445 | |
| 818 |
|
19년 전 | 1610 | |
| 817 |
|
19년 전 | 1531 | |
| 816 |
|
19년 전 | 1449 | |
| 815 |
|
19년 전 | 1464 | |
| 814 |
|
19년 전 | 1386 | |
| 813 |
|
19년 전 | 1451 | |
| 812 | 19년 전 | 2825 | ||
| 811 |
pearly
|
19년 전 | 3842 | |
| 810 |
pearly
|
19년 전 | 5222 | |
| 809 |
|
19년 전 | 1388 | |
| 808 |
pearly
|
19년 전 | 4545 | |
| 807 | 19년 전 | 2983 | ||
| 806 |
|
19년 전 | 1505 | |
| 805 |
|
19년 전 | 2698 | |
| 804 |
|
19년 전 | 3624 | |
| 803 |
|
19년 전 | 1815 | |
| 802 |
|
19년 전 | 3793 | |
| 801 |
|
19년 전 | 1885 | |
| 800 | 19년 전 | 3773 | ||
| 799 | 19년 전 | 3440 | ||
| 798 | 19년 전 | 4211 | ||
| 797 | 19년 전 | 4081 | ||
| 796 |
|
19년 전 | 1899 | |
| 795 |
|
19년 전 | 1860 | |
| 794 | 19년 전 | 4281 | ||
| 793 | 19년 전 | 2675 | ||
| 792 | 19년 전 | 2552 | ||
| 791 | 19년 전 | 2408 | ||
| 790 | 19년 전 | 1986 | ||
| 789 | 19년 전 | 2596 | ||
| 788 | 19년 전 | 2212 | ||
| 787 | 19년 전 | 1854 | ||
| 786 | 19년 전 | 1979 | ||
| 785 | 19년 전 | 1448 | ||
| 784 |
|
19년 전 | 1700 | |
| 783 | 19년 전 | 2844 | ||
| 782 |
|
19년 전 | 1970 | |
| 781 | 19년 전 | 3257 | ||
| 780 | 19년 전 | 3162 | ||
| 779 |
|
19년 전 | 2251 | |
| 778 |
|
19년 전 | 1806 | |
| 777 | 19년 전 | 2847 | ||
| 776 | 19년 전 | 2914 | ||
| 775 | 19년 전 | 4025 | ||
| 774 |
|
19년 전 | 1990 | |
| 773 | 19년 전 | 2627 | ||
| 772 | 19년 전 | 2346 | ||
| 771 | 19년 전 | 3474 | ||
| 770 |
|
19년 전 | 1444 | |
| 769 | 19년 전 | 1508 | ||
| 768 | 19년 전 | 1777 | ||
| 767 | 19년 전 | 2204 | ||
| 766 | 19년 전 | 1837 | ||
| 765 | 19년 전 | 1698 | ||
| 764 |
|
19년 전 | 2117 | |
| 763 |
|
19년 전 | 2230 | |
| 762 |
|
19년 전 | 4965 | |
| 761 | 19년 전 | 2334 | ||
| 760 |
|
19년 전 | 3198 | |
| 759 | 19년 전 | 2646 | ||
| 758 |
|
19년 전 | 2408 | |
| 757 | 19년 전 | 4705 | ||
| 756 | 19년 전 | 2574 | ||
| 755 |
|
19년 전 | 2420 | |
| 754 |
|
19년 전 | 1968 | |
| 753 |
|
19년 전 | 1746 | |
| 752 |
pearly
|
19년 전 | 3374 | |
| 751 | 19년 전 | 2353 | ||
| 750 |
|
19년 전 | 6168 | |
| 749 | 19년 전 | 2043 | ||
| 748 |
|
19년 전 | 1911 | |
| 747 |
|
19년 전 | 2711 | |
| 746 |
|
19년 전 | 1822 | |
| 745 | 19년 전 | 2288 | ||
| 744 | 19년 전 | 2118 | ||
| 743 |
|
19년 전 | 3576 | |
| 742 | 19년 전 | 2587 | ||
| 741 | 19년 전 | 2751 | ||
| 740 |
|
19년 전 | 4377 | |
| 739 | 19년 전 | 3554 | ||
| 738 |
|
19년 전 | 2311 | |
| 737 | 19년 전 | 4320 | ||
| 736 | 19년 전 | 3279 | ||
| 735 |
홀로남은자
|
19년 전 | 4148 | |
| 734 |
홀로남은자
|
19년 전 | 2030 | |
| 733 |
홀로남은자
|
19년 전 | 2236 | |
| 732 | 19년 전 | 2147 | ||
| 731 | 19년 전 | 3358 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기