유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 75 | ||
| 8229 | 9년 전 | 73 | ||
| 8228 |
커네드커네드
|
9년 전 | 115 | |
| 8227 | 9년 전 | 124 | ||
| 8226 | 9년 전 | 164 | ||
| 8225 | 9년 전 | 151 | ||
| 8224 | 9년 전 | 147 | ||
| 8223 | 9년 전 | 114 | ||
| 8222 |
|
9년 전 | 188 | |
| 8221 | 9년 전 | 91 | ||
| 8220 | 9년 전 | 102 | ||
| 8219 | 9년 전 | 101 | ||
| 8218 | 9년 전 | 141 | ||
| 8217 |
star3840
|
9년 전 | 115 | |
| 8216 | 9년 전 | 162 | ||
| 8215 | 9년 전 | 114 | ||
| 8214 | 9년 전 | 228 | ||
| 8213 | 9년 전 | 161 | ||
| 8212 | 9년 전 | 82 | ||
| 8211 | 9년 전 | 245 | ||
| 8210 | 9년 전 | 246 | ||
| 8209 | 9년 전 | 337 | ||
| 8208 | 9년 전 | 217 | ||
| 8207 | 9년 전 | 228 | ||
| 8206 |
|
9년 전 | 191 | |
| 8205 | 9년 전 | 173 | ||
| 8204 | 9년 전 | 133 | ||
| 8203 | 9년 전 | 234 | ||
| 8202 | 9년 전 | 146 | ||
| 8201 | 9년 전 | 182 | ||
| 8200 | 9년 전 | 166 | ||
| 8199 | 9년 전 | 216 | ||
| 8198 | 9년 전 | 179 | ||
| 8197 | 9년 전 | 167 | ||
| 8196 | 9년 전 | 556 | ||
| 8195 | 9년 전 | 157 | ||
| 8194 | 9년 전 | 283 | ||
| 8193 | 9년 전 | 156 | ||
| 8192 | 9년 전 | 190 | ||
| 8191 | 9년 전 | 143 | ||
| 8190 | 9년 전 | 127 | ||
| 8189 | 9년 전 | 193 | ||
| 8188 | 9년 전 | 129 | ||
| 8187 | 9년 전 | 141 | ||
| 8186 | 9년 전 | 143 | ||
| 8185 | 9년 전 | 314 | ||
| 8184 | 9년 전 | 108 | ||
| 8183 | 9년 전 | 324 | ||
| 8182 | 9년 전 | 168 | ||
| 8181 | 9년 전 | 134 | ||
| 8180 | 9년 전 | 693 | ||
| 8179 | 9년 전 | 488 | ||
| 8178 | 9년 전 | 310 | ||
| 8177 |
kiplayer
|
9년 전 | 321 | |
| 8176 | 9년 전 | 355 | ||
| 8175 | 9년 전 | 221 | ||
| 8174 | 9년 전 | 246 | ||
| 8173 | 9년 전 | 345 | ||
| 8172 | 9년 전 | 196 | ||
| 8171 | 9년 전 | 184 | ||
| 8170 | 9년 전 | 298 | ||
| 8169 |
커네드커네드
|
9년 전 | 261 | |
| 8168 | 9년 전 | 322 | ||
| 8167 | 9년 전 | 323 | ||
| 8166 | 9년 전 | 238 | ||
| 8165 | 9년 전 | 167 | ||
| 8164 | 9년 전 | 304 | ||
| 8163 | 9년 전 | 289 | ||
| 8162 | 9년 전 | 303 | ||
| 8161 | 9년 전 | 298 | ||
| 8160 |
|
9년 전 | 490 | |
| 8159 | 9년 전 | 428 | ||
| 8158 | 9년 전 | 253 | ||
| 8157 | 9년 전 | 381 | ||
| 8156 | 9년 전 | 279 | ||
| 8155 | 9년 전 | 254 | ||
| 8154 |
00년생용띠
|
9년 전 | 599 | |
| 8153 | 9년 전 | 234 | ||
| 8152 |
|
9년 전 | 409 | |
| 8151 | 9년 전 | 409 | ||
| 8150 | 9년 전 | 499 | ||
| 8149 |
Jangfolk
|
9년 전 | 351 | |
| 8148 | 9년 전 | 170 | ||
| 8147 | 9년 전 | 373 | ||
| 8146 | 9년 전 | 437 | ||
| 8145 | 9년 전 | 381 | ||
| 8144 | 9년 전 | 348 | ||
| 8143 | 9년 전 | 195 | ||
| 8142 | 9년 전 | 426 | ||
| 8141 | 9년 전 | 383 | ||
| 8140 | 9년 전 | 929 | ||
| 8139 | 9년 전 | 263 | ||
| 8138 |
전갈자리남자
|
9년 전 | 389 | |
| 8137 | 9년 전 | 397 | ||
| 8136 | 9년 전 | 745 | ||
| 8135 |
|
9년 전 | 794 | |
| 8134 |
PlayPixel
|
9년 전 | 517 | |
| 8133 |
|
9년 전 | 438 | |
| 8132 | 9년 전 | 451 | ||
| 8131 | 9년 전 | 812 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기