유용한 함수 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년 전 | 2082 | ||
| 229 | 20년 전 | 3146 | ||
| 228 | 20년 전 | 3338 | ||
| 227 | 20년 전 | 2415 | ||
| 226 | 20년 전 | 5494 | ||
| 225 | 20년 전 | 2533 | ||
| 224 | 20년 전 | 2974 | ||
| 223 | 20년 전 | 4211 | ||
| 222 | 20년 전 | 2612 | ||
| 221 | 20년 전 | 2301 | ||
| 220 | 20년 전 | 3687 | ||
| 219 | 20년 전 | 2083 | ||
| 218 | 20년 전 | 3578 | ||
| 217 | 20년 전 | 2490 | ||
| 216 | 20년 전 | 2926 | ||
| 215 | 20년 전 | 2235 | ||
| 214 | 20년 전 | 3347 | ||
| 213 | 20년 전 | 2926 | ||
| 212 | 20년 전 | 3063 | ||
| 211 | 20년 전 | 2153 | ||
| 210 | 20년 전 | 1910 | ||
| 209 | 20년 전 | 2367 | ||
| 208 | 20년 전 | 1994 | ||
| 207 | 20년 전 | 1686 | ||
| 206 | 20년 전 | 1862 | ||
| 205 | 20년 전 | 3956 | ||
| 204 | 20년 전 | 1683 | ||
| 203 | 20년 전 | 2021 | ||
| 202 | 20년 전 | 2362 | ||
| 201 | 20년 전 | 1837 | ||
| 200 | 20년 전 | 2966 | ||
| 199 | 20년 전 | 2012 | ||
| 198 | 20년 전 | 2094 | ||
| 197 | 20년 전 | 3666 | ||
| 196 | 20년 전 | 3001 | ||
| 195 | 20년 전 | 2110 | ||
| 194 | 20년 전 | 10260 | ||
| 193 | 20년 전 | 2257 | ||
| 192 | 20년 전 | 1613 | ||
| 191 | 20년 전 | 2678 | ||
| 190 | 20년 전 | 2299 | ||
| 189 | 20년 전 | 1698 | ||
| 188 | 20년 전 | 1496 | ||
| 187 | 20년 전 | 1920 | ||
| 186 | 20년 전 | 1743 | ||
| 185 | 20년 전 | 1790 | ||
| 184 | 20년 전 | 2373 | ||
| 183 | 20년 전 | 1579 | ||
| 182 | 20년 전 | 1491 | ||
| 181 | 20년 전 | 1634 | ||
| 180 | 20년 전 | 2732 | ||
| 179 | 20년 전 | 1817 | ||
| 178 | 20년 전 | 1865 | ||
| 177 | 20년 전 | 1995 | ||
| 176 | 20년 전 | 1810 | ||
| 175 | 20년 전 | 1878 | ||
| 174 | 20년 전 | 1703 | ||
| 173 | 20년 전 | 2067 | ||
| 172 | 20년 전 | 1792 | ||
| 171 | 20년 전 | 2572 | ||
| 170 | 20년 전 | 2285 | ||
| 169 | 20년 전 | 2565 | ||
| 168 | 20년 전 | 1498 | ||
| 167 | 20년 전 | 1589 | ||
| 166 | 20년 전 | 2168 | ||
| 165 | 20년 전 | 1648 | ||
| 164 | 20년 전 | 3782 | ||
| 163 | 20년 전 | 2682 | ||
| 162 | 20년 전 | 2102 | ||
| 161 | 20년 전 | 2813 | ||
| 160 | 20년 전 | 1745 | ||
| 159 | 20년 전 | 1624 | ||
| 158 | 20년 전 | 2574 | ||
| 157 | 20년 전 | 1503 | ||
| 156 | 20년 전 | 1754 | ||
| 155 | 20년 전 | 3240 | ||
| 154 | 20년 전 | 1914 | ||
| 153 | 20년 전 | 1636 | ||
| 152 | 20년 전 | 4970 | ||
| 151 | 20년 전 | 4589 | ||
| 150 | 20년 전 | 3526 | ||
| 149 | 20년 전 | 3812 | ||
| 148 | 20년 전 | 7095 | ||
| 147 | 20년 전 | 3564 | ||
| 146 | 20년 전 | 2641 | ||
| 145 | 20년 전 | 2631 | ||
| 144 | 20년 전 | 7175 | ||
| 143 | 20년 전 | 4618 | ||
| 142 | 20년 전 | 1908 | ||
| 141 | 20년 전 | 3256 | ||
| 140 | 20년 전 | 1973 | ||
| 139 | 20년 전 | 1592 | ||
| 138 | 20년 전 | 2317 | ||
| 137 | 20년 전 | 1792 | ||
| 136 | 20년 전 | 1463 | ||
| 135 | 20년 전 | 1802 | ||
| 134 | 20년 전 | 2973 | ||
| 133 | 20년 전 | 2459 | ||
| 132 | 20년 전 | 1713 | ||
| 131 | 20년 전 | 1664 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기