유용한 함수 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년 전 | 4097 | ||
| 429 | 19년 전 | 3296 | ||
| 428 | 19년 전 | 4132 | ||
| 427 | 19년 전 | 3467 | ||
| 426 | 19년 전 | 3101 | ||
| 425 | 19년 전 | 3403 | ||
| 424 | 19년 전 | 2438 | ||
| 423 | 19년 전 | 2860 | ||
| 422 | 19년 전 | 2347 | ||
| 421 | 19년 전 | 3706 | ||
| 420 | 19년 전 | 4734 | ||
| 419 | 19년 전 | 3793 | ||
| 418 |
|
19년 전 | 1741 | |
| 417 | 19년 전 | 2743 | ||
| 416 | 19년 전 | 2387 | ||
| 415 | 19년 전 | 2605 | ||
| 414 | 19년 전 | 4407 | ||
| 413 |
|
19년 전 | 2596 | |
| 412 | 19년 전 | 3045 | ||
| 411 |
|
19년 전 | 2991 | |
| 410 |
|
19년 전 | 3680 | |
| 409 |
|
19년 전 | 3631 | |
| 408 |
|
19년 전 | 1853 | |
| 407 | 19년 전 | 2226 | ||
| 406 | 19년 전 | 2780 | ||
| 405 | 19년 전 | 2442 | ||
| 404 | 19년 전 | 4297 | ||
| 403 | 19년 전 | 3300 | ||
| 402 |
NeoGenesis
|
19년 전 | 4088 | |
| 401 | 19년 전 | 2605 | ||
| 400 |
|
19년 전 | 2491 | |
| 399 | 19년 전 | 2953 | ||
| 398 | 19년 전 | 2483 | ||
| 397 | 19년 전 | 2778 | ||
| 396 | 19년 전 | 2470 | ||
| 395 | 19년 전 | 3176 | ||
| 394 | 19년 전 | 1715 | ||
| 393 | 19년 전 | 2990 | ||
| 392 | 19년 전 | 2249 | ||
| 391 | 19년 전 | 2176 | ||
| 390 | 19년 전 | 2283 | ||
| 389 | 19년 전 | 2642 | ||
| 388 | 19년 전 | 2172 | ||
| 387 | 19년 전 | 4484 | ||
| 386 |
|
19년 전 | 2689 | |
| 385 |
|
19년 전 | 2480 | |
| 384 | 19년 전 | 3007 | ||
| 383 | 19년 전 | 3050 | ||
| 382 | 19년 전 | 3103 | ||
| 381 |
|
19년 전 | 2629 | |
| 380 |
|
19년 전 | 3014 | |
| 379 | 19년 전 | 2545 | ||
| 378 | 19년 전 | 2205 | ||
| 377 | 19년 전 | 2776 | ||
| 376 | 19년 전 | 2467 | ||
| 375 |
|
19년 전 | 2555 | |
| 374 | 19년 전 | 3815 | ||
| 373 | 19년 전 | 3264 | ||
| 372 | 19년 전 | 4985 | ||
| 371 |
세은아빠2
|
19년 전 | 2412 | |
| 370 | 19년 전 | 4495 | ||
| 369 | 19년 전 | 3109 | ||
| 368 | 19년 전 | 2897 | ||
| 367 | 19년 전 | 3722 | ||
| 366 | 19년 전 | 2648 | ||
| 365 | 19년 전 | 3733 | ||
| 364 | 19년 전 | 4009 | ||
| 363 | 19년 전 | 3434 | ||
| 362 | 19년 전 | 3474 | ||
| 361 | 19년 전 | 4106 | ||
| 360 |
hwatta
|
19년 전 | 2359 | |
| 359 | 19년 전 | 5099 | ||
| 358 | 19년 전 | 3654 | ||
| 357 | 19년 전 | 2594 | ||
| 356 |
sdesign1s
|
19년 전 | 2274 | |
| 355 | 20년 전 | 2748 | ||
| 354 | 20년 전 | 3027 | ||
| 353 | 20년 전 | 2787 | ||
| 352 |
|
20년 전 | 5768 | |
| 351 |
|
20년 전 | 2700 | |
| 350 |
|
20년 전 | 4289 | |
| 349 |
hwatta
|
20년 전 | 2179 | |
| 348 | 20년 전 | 7306 | ||
| 347 | 20년 전 | 2412 | ||
| 346 | 20년 전 | 3501 | ||
| 345 | 20년 전 | 4305 | ||
| 344 | 20년 전 | 2642 | ||
| 343 | 20년 전 | 3918 | ||
| 342 | 20년 전 | 3064 | ||
| 341 | 20년 전 | 4084 | ||
| 340 |
|
20년 전 | 5137 | |
| 339 |
|
20년 전 | 4231 | |
| 338 | 20년 전 | 5867 | ||
| 337 | 20년 전 | 2042 | ||
| 336 |
|
20년 전 | 3314 | |
| 335 |
|
20년 전 | 3536 | |
| 334 |
|
20년 전 | 2918 | |
| 333 |
hwatta
|
20년 전 | 2431 | |
| 332 | 20년 전 | 4653 | ||
| 331 | 20년 전 | 2267 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기