유용한 함수 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년 전 | 59 | ||
| 8229 | 9년 전 | 60 | ||
| 8228 |
커네드커네드
|
9년 전 | 106 | |
| 8227 | 9년 전 | 114 | ||
| 8226 | 9년 전 | 149 | ||
| 8225 | 9년 전 | 139 | ||
| 8224 | 9년 전 | 138 | ||
| 8223 | 9년 전 | 108 | ||
| 8222 |
|
9년 전 | 170 | |
| 8221 | 9년 전 | 78 | ||
| 8220 | 9년 전 | 85 | ||
| 8219 | 9년 전 | 92 | ||
| 8218 | 9년 전 | 123 | ||
| 8217 |
star3840
|
9년 전 | 101 | |
| 8216 | 9년 전 | 147 | ||
| 8215 | 9년 전 | 99 | ||
| 8214 | 9년 전 | 215 | ||
| 8213 | 9년 전 | 145 | ||
| 8212 | 9년 전 | 70 | ||
| 8211 | 9년 전 | 233 | ||
| 8210 | 9년 전 | 233 | ||
| 8209 | 9년 전 | 324 | ||
| 8208 | 9년 전 | 200 | ||
| 8207 | 9년 전 | 214 | ||
| 8206 |
|
9년 전 | 173 | |
| 8205 | 9년 전 | 157 | ||
| 8204 | 9년 전 | 120 | ||
| 8203 | 9년 전 | 221 | ||
| 8202 | 9년 전 | 132 | ||
| 8201 | 9년 전 | 168 | ||
| 8200 | 9년 전 | 146 | ||
| 8199 | 9년 전 | 196 | ||
| 8198 | 9년 전 | 162 | ||
| 8197 | 9년 전 | 150 | ||
| 8196 | 9년 전 | 534 | ||
| 8195 | 9년 전 | 145 | ||
| 8194 | 9년 전 | 273 | ||
| 8193 | 9년 전 | 145 | ||
| 8192 | 9년 전 | 173 | ||
| 8191 | 9년 전 | 126 | ||
| 8190 | 9년 전 | 116 | ||
| 8189 | 9년 전 | 177 | ||
| 8188 | 9년 전 | 120 | ||
| 8187 | 9년 전 | 130 | ||
| 8186 | 9년 전 | 134 | ||
| 8185 | 9년 전 | 300 | ||
| 8184 | 9년 전 | 96 | ||
| 8183 | 9년 전 | 313 | ||
| 8182 | 9년 전 | 156 | ||
| 8181 | 9년 전 | 117 | ||
| 8180 | 9년 전 | 685 | ||
| 8179 | 9년 전 | 476 | ||
| 8178 | 9년 전 | 288 | ||
| 8177 |
kiplayer
|
9년 전 | 301 | |
| 8176 | 9년 전 | 338 | ||
| 8175 | 9년 전 | 210 | ||
| 8174 | 9년 전 | 223 | ||
| 8173 | 9년 전 | 331 | ||
| 8172 | 9년 전 | 182 | ||
| 8171 | 9년 전 | 169 | ||
| 8170 | 9년 전 | 285 | ||
| 8169 |
커네드커네드
|
9년 전 | 248 | |
| 8168 | 9년 전 | 307 | ||
| 8167 | 9년 전 | 311 | ||
| 8166 | 9년 전 | 222 | ||
| 8165 | 9년 전 | 153 | ||
| 8164 | 9년 전 | 290 | ||
| 8163 | 9년 전 | 273 | ||
| 8162 | 9년 전 | 285 | ||
| 8161 | 9년 전 | 281 | ||
| 8160 |
|
9년 전 | 478 | |
| 8159 | 9년 전 | 402 | ||
| 8158 | 9년 전 | 222 | ||
| 8157 | 9년 전 | 355 | ||
| 8156 | 9년 전 | 269 | ||
| 8155 | 9년 전 | 244 | ||
| 8154 |
00년생용띠
|
9년 전 | 588 | |
| 8153 | 9년 전 | 220 | ||
| 8152 |
|
9년 전 | 393 | |
| 8151 | 9년 전 | 394 | ||
| 8150 | 9년 전 | 487 | ||
| 8149 |
Jangfolk
|
9년 전 | 329 | |
| 8148 | 9년 전 | 156 | ||
| 8147 | 9년 전 | 362 | ||
| 8146 | 9년 전 | 422 | ||
| 8145 | 9년 전 | 361 | ||
| 8144 | 9년 전 | 327 | ||
| 8143 | 9년 전 | 178 | ||
| 8142 | 9년 전 | 418 | ||
| 8141 | 9년 전 | 366 | ||
| 8140 | 9년 전 | 917 | ||
| 8139 | 9년 전 | 248 | ||
| 8138 |
전갈자리남자
|
9년 전 | 378 | |
| 8137 | 9년 전 | 373 | ||
| 8136 | 9년 전 | 729 | ||
| 8135 |
|
9년 전 | 777 | |
| 8134 |
PlayPixel
|
9년 전 | 494 | |
| 8133 |
|
9년 전 | 427 | |
| 8132 | 9년 전 | 438 | ||
| 8131 | 9년 전 | 799 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기