유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 930 | 19년 전 | 3551 | ||
| 929 |
|
19년 전 | 2717 | |
| 928 |
|
19년 전 | 3461 | |
| 927 |
|
19년 전 | 3450 | |
| 926 |
|
19년 전 | 3152 | |
| 925 | 19년 전 | 5441 | ||
| 924 |
|
19년 전 | 2178 | |
| 923 |
|
19년 전 | 2240 | |
| 922 |
|
19년 전 | 2323 | |
| 921 |
|
19년 전 | 3416 | |
| 920 | 19년 전 | 3716 | ||
| 919 |
|
19년 전 | 3752 | |
| 918 |
|
19년 전 | 2376 | |
| 917 |
|
19년 전 | 2405 | |
| 916 |
|
19년 전 | 2719 | |
| 915 | 19년 전 | 3062 | ||
| 914 | 19년 전 | 2467 | ||
| 913 | 19년 전 | 2632 | ||
| 912 | 19년 전 | 2464 | ||
| 911 | 19년 전 | 2229 | ||
| 910 | 19년 전 | 3255 | ||
| 909 | 19년 전 | 3321 | ||
| 908 | 19년 전 | 3050 | ||
| 907 |
|
19년 전 | 4880 | |
| 906 |
|
19년 전 | 2721 | |
| 905 |
|
19년 전 | 3323 | |
| 904 |
|
19년 전 | 3163 | |
| 903 |
|
19년 전 | 2004 | |
| 902 |
|
19년 전 | 3157 | |
| 901 |
|
19년 전 | 1893 | |
| 900 |
|
19년 전 | 2349 | |
| 899 |
|
19년 전 | 2407 | |
| 898 |
|
19년 전 | 3993 | |
| 897 |
|
19년 전 | 3200 | |
| 896 |
|
19년 전 | 3114 | |
| 895 |
|
19년 전 | 2552 | |
| 894 |
|
19년 전 | 2027 | |
| 893 |
|
19년 전 | 1658 | |
| 892 |
|
19년 전 | 2423 | |
| 891 |
|
19년 전 | 2337 | |
| 890 |
|
19년 전 | 1822 | |
| 889 |
|
19년 전 | 1822 | |
| 888 |
|
19년 전 | 2618 | |
| 887 |
|
19년 전 | 2587 | |
| 886 |
|
19년 전 | 1879 | |
| 885 |
|
19년 전 | 2130 | |
| 884 |
|
19년 전 | 3316 | |
| 883 |
|
19년 전 | 1961 | |
| 882 |
|
19년 전 | 2515 | |
| 881 |
|
19년 전 | 2595 | |
| 880 |
|
19년 전 | 2672 | |
| 879 |
|
19년 전 | 2946 | |
| 878 |
|
19년 전 | 2566 | |
| 877 |
|
19년 전 | 2990 | |
| 876 |
|
19년 전 | 2767 | |
| 875 |
|
19년 전 | 3526 | |
| 874 | 19년 전 | 1877 | ||
| 873 | 19년 전 | 2746 | ||
| 872 |
|
19년 전 | 2383 | |
| 871 |
|
19년 전 | 2026 | |
| 870 |
|
19년 전 | 2432 | |
| 869 |
|
19년 전 | 1879 | |
| 868 |
|
19년 전 | 5449 | |
| 867 |
|
19년 전 | 2277 | |
| 866 |
|
19년 전 | 4275 | |
| 865 |
|
19년 전 | 2312 | |
| 864 | 19년 전 | 1863 | ||
| 863 | 19년 전 | 2621 | ||
| 862 | 19년 전 | 2339 | ||
| 861 | 19년 전 | 2537 | ||
| 860 | 19년 전 | 2173 | ||
| 859 | 19년 전 | 3931 | ||
| 858 | 19년 전 | 3429 | ||
| 857 | 19년 전 | 2212 | ||
| 856 |
Power
|
19년 전 | 2113 | |
| 855 | 19년 전 | 1974 | ||
| 854 | 19년 전 | 2008 | ||
| 853 |
pearly
|
19년 전 | 4408 | |
| 852 |
pearly
|
19년 전 | 3364 | |
| 851 | 19년 전 | 2787 | ||
| 850 |
pearly
|
19년 전 | 3361 | |
| 849 |
pearly
|
19년 전 | 3018 | |
| 848 |
pearly
|
19년 전 | 2858 | |
| 847 | 19년 전 | 2379 | ||
| 846 |
|
19년 전 | 2188 | |
| 845 |
pearly
|
19년 전 | 2507 | |
| 844 | 19년 전 | 3114 | ||
| 843 | 19년 전 | 2143 | ||
| 842 |
pearly
|
19년 전 | 3129 | |
| 841 |
pearly
|
19년 전 | 3255 | |
| 840 | 19년 전 | 2936 | ||
| 839 |
|
19년 전 | 1951 | |
| 838 |
|
19년 전 | 1716 | |
| 837 |
|
19년 전 | 2340 | |
| 836 |
|
19년 전 | 2285 | |
| 835 |
|
19년 전 | 1659 | |
| 834 |
|
19년 전 | 1670 | |
| 833 |
|
19년 전 | 1578 | |
| 832 |
|
19년 전 | 2072 | |
| 831 |
|
19년 전 | 1623 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기