유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1130 | 18년 전 | 2473 | ||
| 1129 | 18년 전 | 2456 | ||
| 1128 | 18년 전 | 2324 | ||
| 1127 | 18년 전 | 2566 | ||
| 1126 |
|
18년 전 | 3866 | |
| 1125 | 18년 전 | 3661 | ||
| 1124 |
|
18년 전 | 2097 | |
| 1123 | 18년 전 | 2031 | ||
| 1122 | 18년 전 | 1580 | ||
| 1121 | 18년 전 | 3904 | ||
| 1120 | 18년 전 | 5895 | ||
| 1119 | 18년 전 | 6992 | ||
| 1118 | 18년 전 | 2473 | ||
| 1117 |
BEST79
|
18년 전 | 2731 | |
| 1116 | 18년 전 | 4188 | ||
| 1115 | 18년 전 | 2178 | ||
| 1114 |
|
18년 전 | 3636 | |
| 1113 | 18년 전 | 2883 | ||
| 1112 | 18년 전 | 2740 | ||
| 1111 | 18년 전 | 2199 | ||
| 1110 | 18년 전 | 2474 | ||
| 1109 | 18년 전 | 2619 | ||
| 1108 | 18년 전 | 3096 | ||
| 1107 | 18년 전 | 3738 | ||
| 1106 | 18년 전 | 3269 | ||
| 1105 | 18년 전 | 2785 | ||
| 1104 |
|
18년 전 | 2275 | |
| 1103 | 18년 전 | 2943 | ||
| 1102 | 18년 전 | 3026 | ||
| 1101 | 18년 전 | 2541 | ||
| 1100 | 18년 전 | 2297 | ||
| 1099 | 18년 전 | 3328 | ||
| 1098 | 18년 전 | 4851 | ||
| 1097 | 18년 전 | 5087 | ||
| 1096 | 18년 전 | 2309 | ||
| 1095 | 18년 전 | 2193 | ||
| 1094 | 18년 전 | 5004 | ||
| 1093 | 18년 전 | 8971 | ||
| 1092 | 18년 전 | 2040 | ||
| 1091 |
DeepnBlue
|
18년 전 | 4922 | |
| 1090 |
|
18년 전 | 4372 | |
| 1089 |
도날드주주
|
18년 전 | 2614 | |
| 1088 |
|
18년 전 | 4963 | |
| 1087 | 18년 전 | 2791 | ||
| 1086 | 18년 전 | 3673 | ||
| 1085 | 18년 전 | 2620 | ||
| 1084 | 18년 전 | 3436 | ||
| 1083 | 18년 전 | 1943 | ||
| 1082 | 18년 전 | 5378 | ||
| 1081 | 18년 전 | 1700 | ||
| 1080 | 18년 전 | 6058 | ||
| 1079 |
|
18년 전 | 2888 | |
| 1078 | 18년 전 | 6046 | ||
| 1077 |
|
18년 전 | 6846 | |
| 1076 | 18년 전 | 3728 | ||
| 1075 | 18년 전 | 2208 | ||
| 1074 |
hwatta
|
18년 전 | 2100 | |
| 1073 | 18년 전 | 6902 | ||
| 1072 | 18년 전 | 2400 | ||
| 1071 | 18년 전 | 3958 | ||
| 1070 |
|
18년 전 | 5349 | |
| 1069 | 18년 전 | 2698 | ||
| 1068 | 18년 전 | 1756 | ||
| 1067 | 18년 전 | 1592 | ||
| 1066 | 18년 전 | 1536 | ||
| 1065 | 18년 전 | 1814 | ||
| 1064 | 18년 전 | 1811 | ||
| 1063 | 18년 전 | 1783 | ||
| 1062 | 18년 전 | 1715 | ||
| 1061 | 18년 전 | 2513 | ||
| 1060 | 18년 전 | 2315 | ||
| 1059 | 18년 전 | 3467 | ||
| 1058 | 18년 전 | 2602 | ||
| 1057 | 18년 전 | 2575 | ||
| 1056 | 18년 전 | 3414 | ||
| 1055 | 18년 전 | 4628 | ||
| 1054 | 18년 전 | 2969 | ||
| 1053 | 18년 전 | 2818 | ||
| 1052 | 18년 전 | 3351 | ||
| 1051 | 18년 전 | 6279 | ||
| 1050 | 18년 전 | 2183 | ||
| 1049 | 18년 전 | 1999 | ||
| 1048 | 18년 전 | 1949 | ||
| 1047 | 18년 전 | 2011 | ||
| 1046 | 18년 전 | 3204 | ||
| 1045 | 18년 전 | 2263 | ||
| 1044 | 18년 전 | 1955 | ||
| 1043 | 18년 전 | 1621 | ||
| 1042 | 18년 전 | 2081 | ||
| 1041 | 18년 전 | 3366 | ||
| 1040 | 18년 전 | 3232 | ||
| 1039 | 18년 전 | 1922 | ||
| 1038 | 18년 전 | 1499 | ||
| 1037 | 18년 전 | 3199 | ||
| 1036 | 18년 전 | 2065 | ||
| 1035 | 18년 전 | 1615 | ||
| 1034 | 18년 전 | 2839 | ||
| 1033 | 18년 전 | 1974 | ||
| 1032 | 18년 전 | 1849 | ||
| 1031 | 18년 전 | 1822 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기