유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6730 |
|
11년 전 | 1103 | |
| 6729 | 11년 전 | 557 | ||
| 6728 |
|
11년 전 | 568 | |
| 6727 | 11년 전 | 2411 | ||
| 6726 | 11년 전 | 593 | ||
| 6725 |
네모웹에이전시
|
11년 전 | 425 | |
| 6724 |
네모웹에이전시
|
11년 전 | 879 | |
| 6723 | 11년 전 | 1011 | ||
| 6722 | 11년 전 | 957 | ||
| 6721 | 11년 전 | 639 | ||
| 6720 | 11년 전 | 1968 | ||
| 6719 | 11년 전 | 2191 | ||
| 6718 | 11년 전 | 1085 | ||
| 6717 |
|
11년 전 | 645 | |
| 6716 | 11년 전 | 2309 | ||
| 6715 | 11년 전 | 7575 | ||
| 6714 | 11년 전 | 2039 | ||
| 6713 | 11년 전 | 772 | ||
| 6712 |
geektoo
|
11년 전 | 1143 | |
| 6711 | 11년 전 | 888 | ||
| 6710 |
sirzzang
|
11년 전 | 2121 | |
| 6709 |
bewitched
|
11년 전 | 1794 | |
| 6708 |
levin
|
11년 전 | 560 | |
| 6707 | 11년 전 | 764 | ||
| 6706 | 11년 전 | 1782 | ||
| 6705 | 11년 전 | 982 | ||
| 6704 |
|
11년 전 | 751 | |
| 6703 | 11년 전 | 411 | ||
| 6702 | 11년 전 | 1086 | ||
| 6701 | 11년 전 | 799 | ||
| 6700 | 11년 전 | 1716 | ||
| 6699 | 11년 전 | 712 | ||
| 6698 |
이박사친구
|
11년 전 | 721 | |
| 6697 | 11년 전 | 1210 | ||
| 6696 | 11년 전 | 615 | ||
| 6695 |
Header
|
11년 전 | 701 | |
| 6694 | 11년 전 | 1156 | ||
| 6693 |
|
11년 전 | 1100 | |
| 6692 | 11년 전 | 1131 | ||
| 6691 | 11년 전 | 1283 | ||
| 6690 |
|
11년 전 | 708 | |
| 6689 | 11년 전 | 889 | ||
| 6688 | 11년 전 | 921 | ||
| 6687 | 11년 전 | 529 | ||
| 6686 |
RGB255
|
11년 전 | 961 | |
| 6685 |
|
11년 전 | 635 | |
| 6684 | 11년 전 | 716 | ||
| 6683 | 11년 전 | 379 | ||
| 6682 | 11년 전 | 1121 | ||
| 6681 | 11년 전 | 1407 | ||
| 6680 | 11년 전 | 432 | ||
| 6679 |
RGB255
|
11년 전 | 378 | |
| 6678 | 11년 전 | 1342 | ||
| 6677 |
|
11년 전 | 425 | |
| 6676 | 11년 전 | 1011 | ||
| 6675 |
디자이너필이
|
11년 전 | 816 | |
| 6674 | 11년 전 | 1239 | ||
| 6673 | 11년 전 | 1310 | ||
| 6672 | 11년 전 | 6017 | ||
| 6671 | 11년 전 | 1331 | ||
| 6670 |
하프의정령
|
11년 전 | 558 | |
| 6669 | 11년 전 | 411 | ||
| 6668 |
공부하고가겠슴다
|
11년 전 | 436 | |
| 6667 |
하프의정령
|
11년 전 | 542 | |
| 6666 | 11년 전 | 781 | ||
| 6665 | 11년 전 | 1406 | ||
| 6664 | 11년 전 | 911 | ||
| 6663 | 11년 전 | 1137 | ||
| 6662 | 11년 전 | 404 | ||
| 6661 |
basketball
|
11년 전 | 504 | |
| 6660 | 11년 전 | 2502 | ||
| 6659 | 11년 전 | 1598 | ||
| 6658 |
|
11년 전 | 1310 | |
| 6657 |
|
11년 전 | 3143 | |
| 6656 | 11년 전 | 556 | ||
| 6655 |
프로프리랜서
|
11년 전 | 2329 | |
| 6654 |
프로프리랜서
|
11년 전 | 1900 | |
| 6653 |
프로프리랜서
|
11년 전 | 1744 | |
| 6652 |
프로프리랜서
|
11년 전 | 1816 | |
| 6651 |
|
11년 전 | 767 | |
| 6650 | 11년 전 | 1310 | ||
| 6649 | 11년 전 | 1312 | ||
| 6648 | 11년 전 | 805 | ||
| 6647 | 11년 전 | 3153 | ||
| 6646 | 11년 전 | 457 | ||
| 6645 | 11년 전 | 957 | ||
| 6644 |
BBAYOUNG
|
11년 전 | 1359 | |
| 6643 | 11년 전 | 1777 | ||
| 6642 | 11년 전 | 630 | ||
| 6641 | 11년 전 | 1326 | ||
| 6640 | 11년 전 | 698 | ||
| 6639 | 11년 전 | 2316 | ||
| 6638 |
jasmin2
|
11년 전 | 741 | |
| 6637 |
geektoo
|
11년 전 | 436 | |
| 6636 | 11년 전 | 648 | ||
| 6635 |
프로프리랜서
|
11년 전 | 2220 | |
| 6634 |
프로프리랜서
|
11년 전 | 1736 | |
| 6633 |
프로프리랜서
|
11년 전 | 4056 | |
| 6632 |
프로프리랜서
|
11년 전 | 1342 | |
| 6631 |
프로프리랜서
|
11년 전 | 1775 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기