유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 230 | 20년 전 | 2085 | ||
| 229 | 20년 전 | 3146 | ||
| 228 | 20년 전 | 3338 | ||
| 227 | 20년 전 | 2415 | ||
| 226 | 20년 전 | 5494 | ||
| 225 | 20년 전 | 2534 | ||
| 224 | 20년 전 | 2975 | ||
| 223 | 20년 전 | 4212 | ||
| 222 | 20년 전 | 2612 | ||
| 221 | 20년 전 | 2302 | ||
| 220 | 20년 전 | 3688 | ||
| 219 | 20년 전 | 2083 | ||
| 218 | 20년 전 | 3579 | ||
| 217 | 20년 전 | 2491 | ||
| 216 | 20년 전 | 2930 | ||
| 215 | 20년 전 | 2237 | ||
| 214 | 20년 전 | 3348 | ||
| 213 | 20년 전 | 2927 | ||
| 212 | 20년 전 | 3066 | ||
| 211 | 20년 전 | 2153 | ||
| 210 | 20년 전 | 1912 | ||
| 209 | 20년 전 | 2369 | ||
| 208 | 20년 전 | 1998 | ||
| 207 | 20년 전 | 1686 | ||
| 206 | 20년 전 | 1862 | ||
| 205 | 20년 전 | 3958 | ||
| 204 | 20년 전 | 1683 | ||
| 203 | 20년 전 | 2021 | ||
| 202 | 20년 전 | 2364 | ||
| 201 | 20년 전 | 1838 | ||
| 200 | 20년 전 | 2969 | ||
| 199 | 20년 전 | 2012 | ||
| 198 | 20년 전 | 2094 | ||
| 197 | 20년 전 | 3667 | ||
| 196 | 20년 전 | 3003 | ||
| 195 | 20년 전 | 2113 | ||
| 194 | 20년 전 | 10261 | ||
| 193 | 20년 전 | 2259 | ||
| 192 | 20년 전 | 1615 | ||
| 191 | 20년 전 | 2678 | ||
| 190 | 20년 전 | 2299 | ||
| 189 | 20년 전 | 1699 | ||
| 188 | 20년 전 | 1498 | ||
| 187 | 20년 전 | 1921 | ||
| 186 | 20년 전 | 1744 | ||
| 185 | 20년 전 | 1792 | ||
| 184 | 20년 전 | 2375 | ||
| 183 | 20년 전 | 1581 | ||
| 182 | 20년 전 | 1492 | ||
| 181 | 20년 전 | 1634 | ||
| 180 | 20년 전 | 2734 | ||
| 179 | 20년 전 | 1819 | ||
| 178 | 20년 전 | 1866 | ||
| 177 | 20년 전 | 2000 | ||
| 176 | 20년 전 | 1810 | ||
| 175 | 20년 전 | 1878 | ||
| 174 | 20년 전 | 1704 | ||
| 173 | 20년 전 | 2067 | ||
| 172 | 20년 전 | 1792 | ||
| 171 | 20년 전 | 2574 | ||
| 170 | 20년 전 | 2288 | ||
| 169 | 20년 전 | 2566 | ||
| 168 | 20년 전 | 1501 | ||
| 167 | 20년 전 | 1593 | ||
| 166 | 20년 전 | 2169 | ||
| 165 | 20년 전 | 1649 | ||
| 164 | 20년 전 | 3784 | ||
| 163 | 20년 전 | 2683 | ||
| 162 | 20년 전 | 2104 | ||
| 161 | 20년 전 | 2813 | ||
| 160 | 20년 전 | 1747 | ||
| 159 | 20년 전 | 1626 | ||
| 158 | 20년 전 | 2578 | ||
| 157 | 20년 전 | 1504 | ||
| 156 | 20년 전 | 1757 | ||
| 155 | 20년 전 | 3242 | ||
| 154 | 20년 전 | 1915 | ||
| 153 | 20년 전 | 1636 | ||
| 152 | 20년 전 | 4971 | ||
| 151 | 20년 전 | 4590 | ||
| 150 | 20년 전 | 3529 | ||
| 149 | 20년 전 | 3813 | ||
| 148 | 20년 전 | 7096 | ||
| 147 | 20년 전 | 3565 | ||
| 146 | 20년 전 | 2641 | ||
| 145 | 20년 전 | 2634 | ||
| 144 | 20년 전 | 7176 | ||
| 143 | 20년 전 | 4620 | ||
| 142 | 20년 전 | 1909 | ||
| 141 | 20년 전 | 3256 | ||
| 140 | 20년 전 | 1974 | ||
| 139 | 20년 전 | 1599 | ||
| 138 | 20년 전 | 2319 | ||
| 137 | 20년 전 | 1793 | ||
| 136 | 20년 전 | 1464 | ||
| 135 | 20년 전 | 1802 | ||
| 134 | 20년 전 | 2974 | ||
| 133 | 20년 전 | 2461 | ||
| 132 | 20년 전 | 1713 | ||
| 131 | 20년 전 | 1664 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기