유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 130 | 20년 전 | 3971 | ||
| 129 | 20년 전 | 2912 | ||
| 128 | 20년 전 | 3646 | ||
| 127 | 20년 전 | 3458 | ||
| 126 | 20년 전 | 3730 | ||
| 125 | 20년 전 | 8562 | ||
| 124 | 20년 전 | 2587 | ||
| 123 | 20년 전 | 3735 | ||
| 122 | 20년 전 | 3194 | ||
| 121 | 20년 전 | 2593 | ||
| 120 | 20년 전 | 2652 | ||
| 119 | 20년 전 | 2567 | ||
| 118 | 20년 전 | 2851 | ||
| 117 |
|
20년 전 | 3038 | |
| 116 | 20년 전 | 5290 | ||
| 115 | 20년 전 | 3898 | ||
| 114 | 20년 전 | 4948 | ||
| 113 | 20년 전 | 6195 | ||
| 112 | 20년 전 | 7317 | ||
| 111 | 20년 전 | 18421 | ||
| 110 | 20년 전 | 6868 | ||
| 109 | 20년 전 | 2868 | ||
| 108 | 20년 전 | 4126 | ||
| 107 |
prosper
|
20년 전 | 2476 | |
| 106 |
prosper
|
20년 전 | 4299 | |
| 105 |
아우겐나이스
|
20년 전 | 2895 | |
| 104 | 20년 전 | 2249 | ||
| 103 | 20년 전 | 2465 | ||
| 102 | 20년 전 | 2225 | ||
| 101 | 20년 전 | 2554 | ||
| 100 | 20년 전 | 1735 | ||
| 99 | 20년 전 | 1558 | ||
| 98 | 20년 전 | 1609 | ||
| 97 | 20년 전 | 2111 | ||
| 96 | 20년 전 | 1863 | ||
| 95 | 20년 전 | 2360 | ||
| 94 | 20년 전 | 3549 | ||
| 93 | 20년 전 | 1547 | ||
| 92 | 20년 전 | 1745 | ||
| 91 | 20년 전 | 3160 | ||
| 90 | 20년 전 | 2333 | ||
| 89 | 20년 전 | 3158 | ||
| 88 | 20년 전 | 2859 | ||
| 87 | 20년 전 | 3284 | ||
| 86 | 20년 전 | 5108 | ||
| 85 | 20년 전 | 2508 | ||
| 84 | 20년 전 | 4811 | ||
| 83 | 20년 전 | 2489 | ||
| 82 | 20년 전 | 3103 | ||
| 81 | 20년 전 | 7609 | ||
| 80 | 20년 전 | 3808 | ||
| 79 | 20년 전 | 3197 | ||
| 78 | 20년 전 | 4679 | ||
| 77 | 20년 전 | 2885 | ||
| 76 | 20년 전 | 6206 | ||
| 75 | 20년 전 | 4462 | ||
| 74 | 20년 전 | 5771 | ||
| 73 | 20년 전 | 3616 | ||
| 72 | 20년 전 | 5955 | ||
| 71 | 20년 전 | 3096 | ||
| 70 | 20년 전 | 2827 | ||
| 69 | 20년 전 | 2623 | ||
| 68 | 20년 전 | 2431 | ||
| 67 | 20년 전 | 2639 | ||
| 66 | 20년 전 | 2659 | ||
| 65 | 20년 전 | 3779 | ||
| 64 | 20년 전 | 2811 | ||
| 63 | 20년 전 | 2445 | ||
| 62 | 20년 전 | 2250 | ||
| 61 | 20년 전 | 3049 | ||
| 60 | 20년 전 | 3120 | ||
| 59 | 20년 전 | 2499 | ||
| 58 | 20년 전 | 2580 | ||
| 57 | 20년 전 | 2954 | ||
| 56 | 20년 전 | 2300 | ||
| 55 | 20년 전 | 2744 | ||
| 54 | 20년 전 | 2111 | ||
| 53 | 20년 전 | 2337 | ||
| 52 | 20년 전 | 2684 | ||
| 51 |
prosper
|
20년 전 | 2336 | |
| 50 |
prosper
|
20년 전 | 2155 | |
| 49 | 20년 전 | 2162 | ||
| 48 | 20년 전 | 2317 | ||
| 47 | 20년 전 | 1916 | ||
| 46 | 20년 전 | 1917 | ||
| 45 | 20년 전 | 2123 | ||
| 44 | 20년 전 | 2349 | ||
| 43 | 21년 전 | 4564 | ||
| 42 |
prosper
|
21년 전 | 2702 | |
| 41 |
prosper
|
21년 전 | 2104 | |
| 40 | 21년 전 | 2167 | ||
| 39 | 21년 전 | 2134 | ||
| 38 | 21년 전 | 2402 | ||
| 37 | 21년 전 | 2551 | ||
| 36 | 21년 전 | 1766 | ||
| 35 | 21년 전 | 4062 | ||
| 34 | 21년 전 | 3839 | ||
| 33 | 21년 전 | 2980 | ||
| 32 |
prosper
|
21년 전 | 2889 | |
| 31 | 21년 전 | 5259 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기