유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 630 | 19년 전 | 2344 | ||
| 629 |
|
19년 전 | 2003 | |
| 628 |
|
19년 전 | 3118 | |
| 627 |
|
19년 전 | 2025 | |
| 626 |
|
19년 전 | 2065 | |
| 625 |
|
19년 전 | 1716 | |
| 624 |
|
19년 전 | 2216 | |
| 623 | 19년 전 | 2192 | ||
| 622 |
|
19년 전 | 2506 | |
| 621 |
|
19년 전 | 2903 | |
| 620 |
|
19년 전 | 2398 | |
| 619 | 19년 전 | 2462 | ||
| 618 | 19년 전 | 3508 | ||
| 617 |
|
19년 전 | 3229 | |
| 616 |
|
19년 전 | 3218 | |
| 615 |
|
19년 전 | 3033 | |
| 614 |
|
19년 전 | 2042 | |
| 613 |
|
19년 전 | 2371 | |
| 612 |
|
19년 전 | 2234 | |
| 611 |
|
19년 전 | 2078 | |
| 610 |
|
19년 전 | 2513 | |
| 609 |
|
19년 전 | 2416 | |
| 608 |
|
19년 전 | 2806 | |
| 607 | 19년 전 | 2189 | ||
| 606 | 19년 전 | 3522 | ||
| 605 | 19년 전 | 1985 | ||
| 604 | 19년 전 | 2589 | ||
| 603 | 19년 전 | 1606 | ||
| 602 |
|
19년 전 | 2831 | |
| 601 | 19년 전 | 3072 | ||
| 600 |
|
19년 전 | 2153 | |
| 599 | 19년 전 | 1992 | ||
| 598 | 19년 전 | 2475 | ||
| 597 | 19년 전 | 2385 | ||
| 596 |
|
19년 전 | 3072 | |
| 595 |
|
19년 전 | 6750 | |
| 594 | 19년 전 | 2703 | ||
| 593 | 19년 전 | 3184 | ||
| 592 |
|
19년 전 | 3086 | |
| 591 |
|
19년 전 | 1925 | |
| 590 | 19년 전 | 3363 | ||
| 589 | 19년 전 | 2204 | ||
| 588 |
|
19년 전 | 2704 | |
| 587 | 19년 전 | 2343 | ||
| 586 |
|
19년 전 | 4357 | |
| 585 | 19년 전 | 2485 | ||
| 584 | 19년 전 | 2750 | ||
| 583 |
|
19년 전 | 3516 | |
| 582 |
|
19년 전 | 3715 | |
| 581 |
|
19년 전 | 3247 | |
| 580 | 19년 전 | 2479 | ||
| 579 | 19년 전 | 3068 | ||
| 578 | 19년 전 | 3958 | ||
| 577 | 19년 전 | 3803 | ||
| 576 | 19년 전 | 1738 | ||
| 575 |
|
19년 전 | 2202 | |
| 574 |
|
19년 전 | 5399 | |
| 573 | 19년 전 | 6710 | ||
| 572 | 19년 전 | 2559 | ||
| 571 | 19년 전 | 2103 | ||
| 570 | 19년 전 | 2601 | ||
| 569 | 19년 전 | 3295 | ||
| 568 | 19년 전 | 3522 | ||
| 567 | 19년 전 | 2981 | ||
| 566 | 19년 전 | 2597 | ||
| 565 |
|
19년 전 | 4713 | |
| 564 |
|
19년 전 | 6725 | |
| 563 |
|
19년 전 | 4993 | |
| 562 |
|
19년 전 | 5943 | |
| 561 |
|
19년 전 | 2689 | |
| 560 |
|
19년 전 | 2532 | |
| 559 |
|
19년 전 | 2219 | |
| 558 |
|
19년 전 | 2295 | |
| 557 | 19년 전 | 4591 | ||
| 556 |
|
19년 전 | 4663 | |
| 555 | 19년 전 | 2499 | ||
| 554 | 19년 전 | 2172 | ||
| 553 | 19년 전 | 2518 | ||
| 552 |
|
19년 전 | 3156 | |
| 551 | 19년 전 | 2935 | ||
| 550 |
|
19년 전 | 1890 | |
| 549 |
|
19년 전 | 1952 | |
| 548 |
|
19년 전 | 3141 | |
| 547 |
|
19년 전 | 2355 | |
| 546 |
|
19년 전 | 3612 | |
| 545 |
|
19년 전 | 2541 | |
| 544 |
|
19년 전 | 1847 | |
| 543 |
|
19년 전 | 2451 | |
| 542 |
|
19년 전 | 1723 | |
| 541 |
|
19년 전 | 1404 | |
| 540 |
|
19년 전 | 1531 | |
| 539 |
|
19년 전 | 1796 | |
| 538 |
|
19년 전 | 1579 | |
| 537 |
|
19년 전 | 1777 | |
| 536 |
|
19년 전 | 1559 | |
| 535 |
|
19년 전 | 1957 | |
| 534 |
|
19년 전 | 1842 | |
| 533 |
|
19년 전 | 1442 | |
| 532 |
|
19년 전 | 1441 | |
| 531 |
|
19년 전 | 1353 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기