유용한 함수 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년 전 | 2094 | ||
| 229 | 20년 전 | 3155 | ||
| 228 | 20년 전 | 3344 | ||
| 227 | 20년 전 | 2419 | ||
| 226 | 20년 전 | 5499 | ||
| 225 | 20년 전 | 2542 | ||
| 224 | 20년 전 | 2986 | ||
| 223 | 20년 전 | 4219 | ||
| 222 | 20년 전 | 2616 | ||
| 221 | 20년 전 | 2305 | ||
| 220 | 20년 전 | 3694 | ||
| 219 | 20년 전 | 2090 | ||
| 218 | 20년 전 | 3589 | ||
| 217 | 20년 전 | 2501 | ||
| 216 | 20년 전 | 2940 | ||
| 215 | 20년 전 | 2250 | ||
| 214 | 20년 전 | 3356 | ||
| 213 | 20년 전 | 2941 | ||
| 212 | 20년 전 | 3085 | ||
| 211 | 20년 전 | 2171 | ||
| 210 | 20년 전 | 1932 | ||
| 209 | 20년 전 | 2384 | ||
| 208 | 20년 전 | 2017 | ||
| 207 | 20년 전 | 1695 | ||
| 206 | 20년 전 | 1869 | ||
| 205 | 20년 전 | 3968 | ||
| 204 | 20년 전 | 1691 | ||
| 203 | 20년 전 | 2033 | ||
| 202 | 20년 전 | 2369 | ||
| 201 | 20년 전 | 1847 | ||
| 200 | 20년 전 | 2976 | ||
| 199 | 20년 전 | 2020 | ||
| 198 | 20년 전 | 2101 | ||
| 197 | 20년 전 | 3674 | ||
| 196 | 20년 전 | 3013 | ||
| 195 | 20년 전 | 2129 | ||
| 194 | 20년 전 | 10274 | ||
| 193 | 20년 전 | 2271 | ||
| 192 | 20년 전 | 1620 | ||
| 191 | 20년 전 | 2685 | ||
| 190 | 20년 전 | 2305 | ||
| 189 | 20년 전 | 1709 | ||
| 188 | 20년 전 | 1506 | ||
| 187 | 20년 전 | 1929 | ||
| 186 | 20년 전 | 1757 | ||
| 185 | 20년 전 | 1803 | ||
| 184 | 20년 전 | 2388 | ||
| 183 | 20년 전 | 1597 | ||
| 182 | 20년 전 | 1502 | ||
| 181 | 20년 전 | 1643 | ||
| 180 | 20년 전 | 2741 | ||
| 179 | 20년 전 | 1829 | ||
| 178 | 20년 전 | 1880 | ||
| 177 | 20년 전 | 2008 | ||
| 176 | 20년 전 | 1817 | ||
| 175 | 20년 전 | 1889 | ||
| 174 | 20년 전 | 1717 | ||
| 173 | 20년 전 | 2074 | ||
| 172 | 20년 전 | 1799 | ||
| 171 | 20년 전 | 2584 | ||
| 170 | 20년 전 | 2301 | ||
| 169 | 20년 전 | 2581 | ||
| 168 | 20년 전 | 1515 | ||
| 167 | 20년 전 | 1603 | ||
| 166 | 20년 전 | 2175 | ||
| 165 | 20년 전 | 1663 | ||
| 164 | 20년 전 | 3794 | ||
| 163 | 20년 전 | 2691 | ||
| 162 | 20년 전 | 2115 | ||
| 161 | 20년 전 | 2823 | ||
| 160 | 20년 전 | 1759 | ||
| 159 | 20년 전 | 1637 | ||
| 158 | 20년 전 | 2590 | ||
| 157 | 20년 전 | 1516 | ||
| 156 | 20년 전 | 1778 | ||
| 155 | 20년 전 | 3257 | ||
| 154 | 20년 전 | 1927 | ||
| 153 | 20년 전 | 1648 | ||
| 152 | 20년 전 | 4983 | ||
| 151 | 20년 전 | 4604 | ||
| 150 | 20년 전 | 3540 | ||
| 149 | 20년 전 | 3817 | ||
| 148 | 20년 전 | 7102 | ||
| 147 | 20년 전 | 3571 | ||
| 146 | 20년 전 | 2651 | ||
| 145 | 20년 전 | 2642 | ||
| 144 | 20년 전 | 7180 | ||
| 143 | 20년 전 | 4626 | ||
| 142 | 20년 전 | 1915 | ||
| 141 | 20년 전 | 3275 | ||
| 140 | 20년 전 | 1992 | ||
| 139 | 20년 전 | 1617 | ||
| 138 | 20년 전 | 2333 | ||
| 137 | 20년 전 | 1810 | ||
| 136 | 20년 전 | 1476 | ||
| 135 | 20년 전 | 1816 | ||
| 134 | 20년 전 | 2994 | ||
| 133 | 20년 전 | 2475 | ||
| 132 | 20년 전 | 1722 | ||
| 131 | 20년 전 | 1669 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기