유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 430 | 19년 전 | 4124 | ||
| 429 | 19년 전 | 3321 | ||
| 428 | 19년 전 | 4140 | ||
| 427 | 19년 전 | 3488 | ||
| 426 | 19년 전 | 3112 | ||
| 425 | 19년 전 | 3427 | ||
| 424 | 19년 전 | 2450 | ||
| 423 | 19년 전 | 2876 | ||
| 422 | 19년 전 | 2366 | ||
| 421 | 19년 전 | 3720 | ||
| 420 | 19년 전 | 4753 | ||
| 419 | 19년 전 | 3825 | ||
| 418 |
|
19년 전 | 1761 | |
| 417 | 19년 전 | 2761 | ||
| 416 | 19년 전 | 2404 | ||
| 415 | 19년 전 | 2613 | ||
| 414 | 19년 전 | 4428 | ||
| 413 |
|
19년 전 | 2623 | |
| 412 | 19년 전 | 3063 | ||
| 411 |
|
19년 전 | 3001 | |
| 410 |
|
19년 전 | 3706 | |
| 409 |
|
19년 전 | 3650 | |
| 408 |
|
19년 전 | 1869 | |
| 407 | 19년 전 | 2241 | ||
| 406 | 19년 전 | 2806 | ||
| 405 | 19년 전 | 2473 | ||
| 404 | 19년 전 | 4322 | ||
| 403 | 19년 전 | 3313 | ||
| 402 |
NeoGenesis
|
19년 전 | 4110 | |
| 401 | 19년 전 | 2618 | ||
| 400 |
|
19년 전 | 2515 | |
| 399 | 19년 전 | 2974 | ||
| 398 | 19년 전 | 2507 | ||
| 397 | 19년 전 | 2798 | ||
| 396 | 19년 전 | 2505 | ||
| 395 | 19년 전 | 3209 | ||
| 394 | 19년 전 | 1749 | ||
| 393 | 19년 전 | 3007 | ||
| 392 | 19년 전 | 2283 | ||
| 391 | 19년 전 | 2189 | ||
| 390 | 19년 전 | 2299 | ||
| 389 | 19년 전 | 2658 | ||
| 388 | 19년 전 | 2202 | ||
| 387 | 19년 전 | 4493 | ||
| 386 |
|
19년 전 | 2699 | |
| 385 |
|
19년 전 | 2508 | |
| 384 | 19년 전 | 3042 | ||
| 383 | 19년 전 | 3073 | ||
| 382 | 19년 전 | 3122 | ||
| 381 |
|
19년 전 | 2648 | |
| 380 |
|
19년 전 | 3031 | |
| 379 | 19년 전 | 2557 | ||
| 378 | 19년 전 | 2226 | ||
| 377 | 19년 전 | 2790 | ||
| 376 | 19년 전 | 2476 | ||
| 375 |
|
19년 전 | 2574 | |
| 374 | 19년 전 | 3834 | ||
| 373 | 19년 전 | 3285 | ||
| 372 | 19년 전 | 5005 | ||
| 371 |
세은아빠2
|
19년 전 | 2426 | |
| 370 | 19년 전 | 4508 | ||
| 369 | 19년 전 | 3120 | ||
| 368 | 19년 전 | 2920 | ||
| 367 | 19년 전 | 3745 | ||
| 366 | 19년 전 | 2671 | ||
| 365 | 19년 전 | 3756 | ||
| 364 | 19년 전 | 4027 | ||
| 363 | 19년 전 | 3462 | ||
| 362 | 19년 전 | 3497 | ||
| 361 | 19년 전 | 4120 | ||
| 360 |
hwatta
|
19년 전 | 2379 | |
| 359 | 19년 전 | 5123 | ||
| 358 | 20년 전 | 3670 | ||
| 357 | 20년 전 | 2615 | ||
| 356 |
sdesign1s
|
20년 전 | 2294 | |
| 355 | 20년 전 | 2775 | ||
| 354 | 20년 전 | 3040 | ||
| 353 | 20년 전 | 2807 | ||
| 352 |
|
20년 전 | 5787 | |
| 351 |
|
20년 전 | 2722 | |
| 350 |
|
20년 전 | 4310 | |
| 349 |
hwatta
|
20년 전 | 2198 | |
| 348 | 20년 전 | 7319 | ||
| 347 | 20년 전 | 2424 | ||
| 346 | 20년 전 | 3518 | ||
| 345 | 20년 전 | 4325 | ||
| 344 | 20년 전 | 2668 | ||
| 343 | 20년 전 | 3933 | ||
| 342 | 20년 전 | 3080 | ||
| 341 | 20년 전 | 4111 | ||
| 340 |
|
20년 전 | 5165 | |
| 339 |
|
20년 전 | 4249 | |
| 338 | 20년 전 | 5896 | ||
| 337 | 20년 전 | 2056 | ||
| 336 |
|
20년 전 | 3345 | |
| 335 |
|
20년 전 | 3563 | |
| 334 |
|
20년 전 | 2959 | |
| 333 |
hwatta
|
20년 전 | 2463 | |
| 332 | 20년 전 | 4667 | ||
| 331 | 20년 전 | 2292 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기