유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 230 | 20년 전 | 2036 | ||
| 229 | 20년 전 | 3131 | ||
| 228 | 20년 전 | 3322 | ||
| 227 | 20년 전 | 2383 | ||
| 226 | 20년 전 | 5458 | ||
| 225 | 20년 전 | 2506 | ||
| 224 | 20년 전 | 2951 | ||
| 223 | 20년 전 | 4195 | ||
| 222 | 20년 전 | 2595 | ||
| 221 | 20년 전 | 2277 | ||
| 220 | 20년 전 | 3665 | ||
| 219 | 20년 전 | 2062 | ||
| 218 | 20년 전 | 3550 | ||
| 217 | 20년 전 | 2468 | ||
| 216 | 20년 전 | 2895 | ||
| 215 | 20년 전 | 2203 | ||
| 214 | 20년 전 | 3324 | ||
| 213 | 20년 전 | 2909 | ||
| 212 | 20년 전 | 3038 | ||
| 211 | 20년 전 | 2137 | ||
| 210 | 20년 전 | 1868 | ||
| 209 | 20년 전 | 2331 | ||
| 208 | 20년 전 | 1961 | ||
| 207 | 20년 전 | 1661 | ||
| 206 | 20년 전 | 1848 | ||
| 205 | 20년 전 | 3937 | ||
| 204 | 20년 전 | 1666 | ||
| 203 | 20년 전 | 2000 | ||
| 202 | 20년 전 | 2339 | ||
| 201 | 20년 전 | 1813 | ||
| 200 | 20년 전 | 2942 | ||
| 199 | 20년 전 | 1997 | ||
| 198 | 20년 전 | 2077 | ||
| 197 | 20년 전 | 3641 | ||
| 196 | 20년 전 | 2961 | ||
| 195 | 20년 전 | 2084 | ||
| 194 | 20년 전 | 10227 | ||
| 193 | 20년 전 | 2235 | ||
| 192 | 20년 전 | 1579 | ||
| 191 | 20년 전 | 2648 | ||
| 190 | 20년 전 | 2280 | ||
| 189 | 20년 전 | 1671 | ||
| 188 | 20년 전 | 1463 | ||
| 187 | 20년 전 | 1893 | ||
| 186 | 20년 전 | 1692 | ||
| 185 | 20년 전 | 1726 | ||
| 184 | 20년 전 | 2326 | ||
| 183 | 20년 전 | 1538 | ||
| 182 | 20년 전 | 1473 | ||
| 181 | 20년 전 | 1612 | ||
| 180 | 20년 전 | 2708 | ||
| 179 | 20년 전 | 1786 | ||
| 178 | 20년 전 | 1841 | ||
| 177 | 20년 전 | 1967 | ||
| 176 | 20년 전 | 1790 | ||
| 175 | 20년 전 | 1862 | ||
| 174 | 20년 전 | 1684 | ||
| 173 | 20년 전 | 2053 | ||
| 172 | 20년 전 | 1771 | ||
| 171 | 20년 전 | 2550 | ||
| 170 | 20년 전 | 2257 | ||
| 169 | 20년 전 | 2536 | ||
| 168 | 20년 전 | 1464 | ||
| 167 | 20년 전 | 1564 | ||
| 166 | 20년 전 | 2138 | ||
| 165 | 20년 전 | 1601 | ||
| 164 | 20년 전 | 3747 | ||
| 163 | 20년 전 | 2636 | ||
| 162 | 20년 전 | 2062 | ||
| 161 | 20년 전 | 2767 | ||
| 160 | 20년 전 | 1713 | ||
| 159 | 20년 전 | 1590 | ||
| 158 | 20년 전 | 2542 | ||
| 157 | 20년 전 | 1468 | ||
| 156 | 20년 전 | 1712 | ||
| 155 | 20년 전 | 3218 | ||
| 154 | 20년 전 | 1875 | ||
| 153 | 20년 전 | 1604 | ||
| 152 | 20년 전 | 4930 | ||
| 151 | 20년 전 | 4560 | ||
| 150 | 20년 전 | 3498 | ||
| 149 | 20년 전 | 3770 | ||
| 148 | 20년 전 | 7050 | ||
| 147 | 20년 전 | 3532 | ||
| 146 | 20년 전 | 2598 | ||
| 145 | 20년 전 | 2597 | ||
| 144 | 20년 전 | 7155 | ||
| 143 | 20년 전 | 4579 | ||
| 142 | 20년 전 | 1885 | ||
| 141 | 20년 전 | 3242 | ||
| 140 | 20년 전 | 1940 | ||
| 139 | 20년 전 | 1535 | ||
| 138 | 20년 전 | 2287 | ||
| 137 | 20년 전 | 1766 | ||
| 136 | 20년 전 | 1441 | ||
| 135 | 20년 전 | 1777 | ||
| 134 | 20년 전 | 2953 | ||
| 133 | 20년 전 | 2427 | ||
| 132 | 20년 전 | 1702 | ||
| 131 | 20년 전 | 1629 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기