유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6030 | 12년 전 | 2824 | ||
| 6029 | 12년 전 | 2850 | ||
| 6028 | 12년 전 | 1475 | ||
| 6027 | 12년 전 | 893 | ||
| 6026 |
kiplayer
|
12년 전 | 802 | |
| 6025 | 12년 전 | 593 | ||
| 6024 | 12년 전 | 2814 | ||
| 6023 | 12년 전 | 2953 | ||
| 6022 |
|
12년 전 | 2109 | |
| 6021 |
Holyreign
|
12년 전 | 1339 | |
| 6020 | 12년 전 | 786 | ||
| 6019 | 12년 전 | 2862 | ||
| 6018 | 12년 전 | 3193 | ||
| 6017 | 12년 전 | 1242 | ||
| 6016 |
kiplayer
|
12년 전 | 10576 | |
| 6015 |
itlang
|
12년 전 | 1420 | |
| 6014 | 12년 전 | 426 | ||
| 6013 |
senseme
|
12년 전 | 1364 | |
| 6012 | 12년 전 | 3115 | ||
| 6011 | 12년 전 | 7132 | ||
| 6010 | 12년 전 | 8965 | ||
| 6009 | 12년 전 | 2869 | ||
| 6008 | 12년 전 | 2992 | ||
| 6007 | 12년 전 | 6677 | ||
| 6006 | 12년 전 | 10516 | ||
| 6005 | 12년 전 | 4053 | ||
| 6004 |
|
12년 전 | 582 | |
| 6003 | 12년 전 | 887 | ||
| 6002 | 12년 전 | 2353 | ||
| 6001 | 12년 전 | 688 | ||
| 6000 |
kiplayer
|
12년 전 | 3742 | |
| 5999 | 12년 전 | 436 | ||
| 5998 |
|
12년 전 | 2942 | |
| 5997 |
|
12년 전 | 3441 | |
| 5996 | 12년 전 | 2095 | ||
| 5995 |
kiplayer
|
12년 전 | 2174 | |
| 5994 |
|
12년 전 | 3269 | |
| 5993 |
windowhan
|
12년 전 | 677 | |
| 5992 | 12년 전 | 2903 | ||
| 5991 | 12년 전 | 5234 | ||
| 5990 | 12년 전 | 1642 | ||
| 5989 |
AMDbest
|
12년 전 | 1352 | |
| 5988 | 12년 전 | 1220 | ||
| 5987 |
kiplayer
|
12년 전 | 1137 | |
| 5986 |
한번잘해보자
|
12년 전 | 928 | |
| 5985 |
kiplayer
|
12년 전 | 3141 | |
| 5984 |
dsv421
|
12년 전 | 807 | |
| 5983 |
dsv421
|
12년 전 | 1509 | |
| 5982 |
dsv421
|
12년 전 | 5837 | |
| 5981 | 12년 전 | 3214 | ||
| 5980 |
MFBob
|
12년 전 | 2623 | |
| 5979 | 12년 전 | 3100 | ||
| 5978 |
AMDbest
|
12년 전 | 1461 | |
| 5977 | 12년 전 | 793 | ||
| 5976 | 12년 전 | 1210 | ||
| 5975 | 12년 전 | 1274 | ||
| 5974 | 12년 전 | 612 | ||
| 5973 |
kiplayer
|
12년 전 | 3550 | |
| 5972 |
kiplayer
|
12년 전 | 3977 | |
| 5971 |
kiplayer
|
12년 전 | 2299 | |
| 5970 |
kiplayer
|
12년 전 | 8291 | |
| 5969 |
kiplayer
|
12년 전 | 949 | |
| 5968 | 12년 전 | 8741 | ||
| 5967 |
|
12년 전 | 16904 | |
| 5966 | 12년 전 | 1228 | ||
| 5965 | 12년 전 | 708 | ||
| 5964 | 12년 전 | 6247 | ||
| 5963 | 12년 전 | 705 | ||
| 5962 | 12년 전 | 885 | ||
| 5961 | 12년 전 | 636 | ||
| 5960 | 12년 전 | 787 | ||
| 5959 |
|
12년 전 | 1124 | |
| 5958 | 12년 전 | 978 | ||
| 5957 |
웹디자인되고파
|
12년 전 | 1090 | |
| 5956 | 12년 전 | 3658 | ||
| 5955 | 12년 전 | 2140 | ||
| 5954 | 12년 전 | 4396 | ||
| 5953 | 12년 전 | 3087 | ||
| 5952 | 12년 전 | 4267 | ||
| 5951 | 12년 전 | 2075 | ||
| 5950 | 12년 전 | 1277 | ||
| 5949 | 12년 전 | 931 | ||
| 5948 | 12년 전 | 2076 | ||
| 5947 | 12년 전 | 5001 | ||
| 5946 |
dsv421
|
12년 전 | 1274 | |
| 5945 |
dsv421
|
12년 전 | 4120 | |
| 5944 |
dsv421
|
12년 전 | 5645 | |
| 5943 | 12년 전 | 1284 | ||
| 5942 | 12년 전 | 2539 | ||
| 5941 |
프로프리랜서
|
12년 전 | 1644 | |
| 5940 |
atria
|
12년 전 | 614 | |
| 5939 | 12년 전 | 2340 | ||
| 5938 | 12년 전 | 2080 | ||
| 5937 |
프로프리랜서
|
12년 전 | 766 | |
| 5936 |
프로프리랜서
|
12년 전 | 713 | |
| 5935 |
프로프리랜서
|
12년 전 | 3213 | |
| 5934 |
프로프리랜서
|
12년 전 | 1629 | |
| 5933 |
AMDbest
|
12년 전 | 648 | |
| 5932 |
AMDbest
|
12년 전 | 502 | |
| 5931 | 12년 전 | 8141 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기