유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 430 | 19년 전 | 4113 | ||
| 429 | 19년 전 | 3308 | ||
| 428 | 19년 전 | 4134 | ||
| 427 | 19년 전 | 3480 | ||
| 426 | 19년 전 | 3108 | ||
| 425 | 19년 전 | 3411 | ||
| 424 | 19년 전 | 2441 | ||
| 423 | 19년 전 | 2869 | ||
| 422 | 19년 전 | 2356 | ||
| 421 | 19년 전 | 3710 | ||
| 420 | 19년 전 | 4744 | ||
| 419 | 19년 전 | 3806 | ||
| 418 |
|
19년 전 | 1753 | |
| 417 | 19년 전 | 2754 | ||
| 416 | 19년 전 | 2395 | ||
| 415 | 19년 전 | 2610 | ||
| 414 | 19년 전 | 4416 | ||
| 413 |
|
19년 전 | 2606 | |
| 412 | 19년 전 | 3053 | ||
| 411 |
|
19년 전 | 2997 | |
| 410 |
|
19년 전 | 3693 | |
| 409 |
|
19년 전 | 3643 | |
| 408 |
|
19년 전 | 1861 | |
| 407 | 19년 전 | 2235 | ||
| 406 | 19년 전 | 2789 | ||
| 405 | 19년 전 | 2458 | ||
| 404 | 19년 전 | 4311 | ||
| 403 | 19년 전 | 3306 | ||
| 402 |
NeoGenesis
|
19년 전 | 4097 | |
| 401 | 19년 전 | 2611 | ||
| 400 |
|
19년 전 | 2503 | |
| 399 | 19년 전 | 2962 | ||
| 398 | 19년 전 | 2490 | ||
| 397 | 19년 전 | 2788 | ||
| 396 | 19년 전 | 2487 | ||
| 395 | 19년 전 | 3192 | ||
| 394 | 19년 전 | 1733 | ||
| 393 | 19년 전 | 2997 | ||
| 392 | 19년 전 | 2267 | ||
| 391 | 19년 전 | 2181 | ||
| 390 | 19년 전 | 2291 | ||
| 389 | 19년 전 | 2648 | ||
| 388 | 19년 전 | 2187 | ||
| 387 | 19년 전 | 4488 | ||
| 386 |
|
19년 전 | 2694 | |
| 385 |
|
19년 전 | 2494 | |
| 384 | 19년 전 | 3029 | ||
| 383 | 19년 전 | 3061 | ||
| 382 | 19년 전 | 3114 | ||
| 381 |
|
19년 전 | 2638 | |
| 380 |
|
19년 전 | 3026 | |
| 379 | 19년 전 | 2551 | ||
| 378 | 19년 전 | 2213 | ||
| 377 | 19년 전 | 2783 | ||
| 376 | 19년 전 | 2472 | ||
| 375 |
|
19년 전 | 2567 | |
| 374 | 19년 전 | 3823 | ||
| 373 | 19년 전 | 3276 | ||
| 372 | 19년 전 | 4996 | ||
| 371 |
세은아빠2
|
19년 전 | 2420 | |
| 370 | 19년 전 | 4504 | ||
| 369 | 19년 전 | 3109 | ||
| 368 | 19년 전 | 2910 | ||
| 367 | 19년 전 | 3730 | ||
| 366 | 19년 전 | 2661 | ||
| 365 | 19년 전 | 3748 | ||
| 364 | 19년 전 | 4023 | ||
| 363 | 19년 전 | 3450 | ||
| 362 | 19년 전 | 3489 | ||
| 361 | 19년 전 | 4110 | ||
| 360 |
hwatta
|
19년 전 | 2367 | |
| 359 | 19년 전 | 5112 | ||
| 358 | 19년 전 | 3660 | ||
| 357 | 19년 전 | 2608 | ||
| 356 |
sdesign1s
|
20년 전 | 2285 | |
| 355 | 20년 전 | 2760 | ||
| 354 | 20년 전 | 3031 | ||
| 353 | 20년 전 | 2798 | ||
| 352 |
|
20년 전 | 5777 | |
| 351 |
|
20년 전 | 2710 | |
| 350 |
|
20년 전 | 4300 | |
| 349 |
hwatta
|
20년 전 | 2187 | |
| 348 | 20년 전 | 7311 | ||
| 347 | 20년 전 | 2418 | ||
| 346 | 20년 전 | 3509 | ||
| 345 | 20년 전 | 4316 | ||
| 344 | 20년 전 | 2659 | ||
| 343 | 20년 전 | 3925 | ||
| 342 | 20년 전 | 3073 | ||
| 341 | 20년 전 | 4098 | ||
| 340 |
|
20년 전 | 5148 | |
| 339 |
|
20년 전 | 4241 | |
| 338 | 20년 전 | 5882 | ||
| 337 | 20년 전 | 2048 | ||
| 336 |
|
20년 전 | 3329 | |
| 335 |
|
20년 전 | 3545 | |
| 334 |
|
20년 전 | 2938 | |
| 333 |
hwatta
|
20년 전 | 2444 | |
| 332 | 20년 전 | 4658 | ||
| 331 | 20년 전 | 2282 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기