유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1330 | 18년 전 | 1486 | ||
| 1329 | 18년 전 | 1718 | ||
| 1328 | 18년 전 | 1000 | ||
| 1327 | 18년 전 | 1422 | ||
| 1326 | 18년 전 | 2257 | ||
| 1325 | 18년 전 | 1142 | ||
| 1324 | 18년 전 | 1303 | ||
| 1323 | 18년 전 | 1567 | ||
| 1322 | 18년 전 | 1176 | ||
| 1321 | 18년 전 | 968 | ||
| 1320 | 18년 전 | 1194 | ||
| 1319 | 18년 전 | 1521 | ||
| 1318 | 18년 전 | 1648 | ||
| 1317 | 18년 전 | 1600 | ||
| 1316 | 18년 전 | 1210 | ||
| 1315 |
|
18년 전 | 8601 | |
| 1314 | 18년 전 | 1908 | ||
| 1313 | 18년 전 | 1495 | ||
| 1312 | 18년 전 | 1486 | ||
| 1311 | 18년 전 | 1782 | ||
| 1310 | 18년 전 | 1835 | ||
| 1309 | 18년 전 | 1224 | ||
| 1308 | 18년 전 | 985 | ||
| 1307 | 18년 전 | 2759 | ||
| 1306 | 18년 전 | 3109 | ||
| 1305 | 18년 전 | 2170 | ||
| 1304 | 18년 전 | 1556 | ||
| 1303 | 18년 전 | 1421 | ||
| 1302 | 18년 전 | 888 | ||
| 1301 | 18년 전 | 1778 | ||
| 1300 | 18년 전 | 5138 | ||
| 1299 | 18년 전 | 2856 | ||
| 1298 |
mixdesign
|
18년 전 | 2109 | |
| 1297 | 18년 전 | 3079 | ||
| 1296 | 18년 전 | 2545 | ||
| 1295 | 18년 전 | 2240 | ||
| 1294 |
onweb
|
18년 전 | 4560 | |
| 1293 |
onweb
|
18년 전 | 3491 | |
| 1292 | 18년 전 | 3456 | ||
| 1291 | 18년 전 | 4040 | ||
| 1290 | 18년 전 | 1905 | ||
| 1289 | 18년 전 | 1760 | ||
| 1288 | 18년 전 | 1731 | ||
| 1287 | 18년 전 | 1428 | ||
| 1286 | 18년 전 | 1264 | ||
| 1285 | 18년 전 | 1158 | ||
| 1284 | 18년 전 | 1159 | ||
| 1283 | 18년 전 | 1514 | ||
| 1282 | 18년 전 | 915 | ||
| 1281 | 18년 전 | 1271 | ||
| 1280 | 18년 전 | 1129 | ||
| 1279 | 18년 전 | 1147 | ||
| 1278 | 18년 전 | 3026 | ||
| 1277 | 18년 전 | 947 | ||
| 1276 | 18년 전 | 1156 | ||
| 1275 | 18년 전 | 2096 | ||
| 1274 | 18년 전 | 2225 | ||
| 1273 | 18년 전 | 1321 | ||
| 1272 | 18년 전 | 1913 | ||
| 1271 | 18년 전 | 1162 | ||
| 1270 | 18년 전 | 2497 | ||
| 1269 | 18년 전 | 6597 | ||
| 1268 |
페이지팩트
|
18년 전 | 3820 | |
| 1267 |
페이지팩트
|
18년 전 | 2227 | |
| 1266 |
|
18년 전 | 6294 | |
| 1265 | 18년 전 | 2584 | ||
| 1264 | 18년 전 | 2619 | ||
| 1263 | 18년 전 | 2122 | ||
| 1262 | 18년 전 | 2117 | ||
| 1261 |
페이지팩트
|
18년 전 | 2124 | |
| 1260 | 18년 전 | 3886 | ||
| 1259 |
|
18년 전 | 3935 | |
| 1258 |
|
18년 전 | 5038 | |
| 1257 | 18년 전 | 4138 | ||
| 1256 | 18년 전 | 2574 | ||
| 1255 | 18년 전 | 1810 | ||
| 1254 | 18년 전 | 2781 | ||
| 1253 | 18년 전 | 2716 | ||
| 1252 | 18년 전 | 2238 | ||
| 1251 | 18년 전 | 5416 | ||
| 1250 |
jbsstyle
|
18년 전 | 4763 | |
| 1249 |
|
18년 전 | 3772 | |
| 1248 | 18년 전 | 3453 | ||
| 1247 | 18년 전 | 2139 | ||
| 1246 | 18년 전 | 2136 | ||
| 1245 | 18년 전 | 3640 | ||
| 1244 |
|
18년 전 | 2282 | |
| 1243 |
|
18년 전 | 2494 | |
| 1242 | 18년 전 | 1767 | ||
| 1241 | 18년 전 | 4464 | ||
| 1240 | 18년 전 | 2153 | ||
| 1239 | 18년 전 | 2303 | ||
| 1238 | 18년 전 | 4042 | ||
| 1237 |
hwatta
|
18년 전 | 3893 | |
| 1236 | 18년 전 | 2117 | ||
| 1235 | 18년 전 | 2768 | ||
| 1234 | 18년 전 | 5145 | ||
| 1233 | 18년 전 | 2410 | ||
| 1232 |
|
18년 전 | 2952 | |
| 1231 | 18년 전 | 8502 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기