유용한 함수 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년 전 | 4099 | ||
| 429 | 19년 전 | 3299 | ||
| 428 | 19년 전 | 4132 | ||
| 427 | 19년 전 | 3467 | ||
| 426 | 19년 전 | 3103 | ||
| 425 | 19년 전 | 3403 | ||
| 424 | 19년 전 | 2438 | ||
| 423 | 19년 전 | 2862 | ||
| 422 | 19년 전 | 2350 | ||
| 421 | 19년 전 | 3708 | ||
| 420 | 19년 전 | 4739 | ||
| 419 | 19년 전 | 3797 | ||
| 418 |
|
19년 전 | 1746 | |
| 417 | 19년 전 | 2745 | ||
| 416 | 19년 전 | 2390 | ||
| 415 | 19년 전 | 2606 | ||
| 414 | 19년 전 | 4410 | ||
| 413 |
|
19년 전 | 2597 | |
| 412 | 19년 전 | 3046 | ||
| 411 |
|
19년 전 | 2994 | |
| 410 |
|
19년 전 | 3683 | |
| 409 |
|
19년 전 | 3632 | |
| 408 |
|
19년 전 | 1857 | |
| 407 | 19년 전 | 2228 | ||
| 406 | 19년 전 | 2781 | ||
| 405 | 19년 전 | 2444 | ||
| 404 | 19년 전 | 4301 | ||
| 403 | 19년 전 | 3302 | ||
| 402 |
NeoGenesis
|
19년 전 | 4089 | |
| 401 | 19년 전 | 2606 | ||
| 400 |
|
19년 전 | 2493 | |
| 399 | 19년 전 | 2956 | ||
| 398 | 19년 전 | 2484 | ||
| 397 | 19년 전 | 2779 | ||
| 396 | 19년 전 | 2473 | ||
| 395 | 19년 전 | 3177 | ||
| 394 | 19년 전 | 1718 | ||
| 393 | 19년 전 | 2993 | ||
| 392 | 19년 전 | 2251 | ||
| 391 | 19년 전 | 2176 | ||
| 390 | 19년 전 | 2285 | ||
| 389 | 19년 전 | 2644 | ||
| 388 | 19년 전 | 2176 | ||
| 387 | 19년 전 | 4484 | ||
| 386 |
|
19년 전 | 2692 | |
| 385 |
|
19년 전 | 2481 | |
| 384 | 19년 전 | 3012 | ||
| 383 | 19년 전 | 3052 | ||
| 382 | 19년 전 | 3107 | ||
| 381 |
|
19년 전 | 2631 | |
| 380 |
|
19년 전 | 3018 | |
| 379 | 19년 전 | 2549 | ||
| 378 | 19년 전 | 2206 | ||
| 377 | 19년 전 | 2776 | ||
| 376 | 19년 전 | 2468 | ||
| 375 |
|
19년 전 | 2561 | |
| 374 | 19년 전 | 3818 | ||
| 373 | 19년 전 | 3267 | ||
| 372 | 19년 전 | 4986 | ||
| 371 |
세은아빠2
|
19년 전 | 2415 | |
| 370 | 19년 전 | 4499 | ||
| 369 | 19년 전 | 3109 | ||
| 368 | 19년 전 | 2905 | ||
| 367 | 19년 전 | 3723 | ||
| 366 | 19년 전 | 2652 | ||
| 365 | 19년 전 | 3735 | ||
| 364 | 19년 전 | 4009 | ||
| 363 | 19년 전 | 3437 | ||
| 362 | 19년 전 | 3476 | ||
| 361 | 19년 전 | 4107 | ||
| 360 |
hwatta
|
19년 전 | 2361 | |
| 359 | 19년 전 | 5101 | ||
| 358 | 19년 전 | 3655 | ||
| 357 | 19년 전 | 2596 | ||
| 356 |
sdesign1s
|
19년 전 | 2276 | |
| 355 | 20년 전 | 2754 | ||
| 354 | 20년 전 | 3028 | ||
| 353 | 20년 전 | 2789 | ||
| 352 |
|
20년 전 | 5771 | |
| 351 |
|
20년 전 | 2701 | |
| 350 |
|
20년 전 | 4292 | |
| 349 |
hwatta
|
20년 전 | 2181 | |
| 348 | 20년 전 | 7306 | ||
| 347 | 20년 전 | 2413 | ||
| 346 | 20년 전 | 3503 | ||
| 345 | 20년 전 | 4306 | ||
| 344 | 20년 전 | 2644 | ||
| 343 | 20년 전 | 3918 | ||
| 342 | 20년 전 | 3066 | ||
| 341 | 20년 전 | 4090 | ||
| 340 |
|
20년 전 | 5140 | |
| 339 |
|
20년 전 | 4234 | |
| 338 | 20년 전 | 5868 | ||
| 337 | 20년 전 | 2043 | ||
| 336 |
|
20년 전 | 3318 | |
| 335 |
|
20년 전 | 3537 | |
| 334 |
|
20년 전 | 2924 | |
| 333 |
hwatta
|
20년 전 | 2433 | |
| 332 | 20년 전 | 4655 | ||
| 331 | 20년 전 | 2268 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기