유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7630 | 10년 전 | 647 | ||
| 7629 |
|
10년 전 | 2375 | |
| 7628 | 10년 전 | 784 | ||
| 7627 |
|
10년 전 | 1016 | |
| 7626 |
|
10년 전 | 1782 | |
| 7625 | 10년 전 | 689 | ||
| 7624 | 10년 전 | 706 | ||
| 7623 |
|
10년 전 | 3070 | |
| 7622 | 10년 전 | 717 | ||
| 7621 |
leeleeleelee
|
10년 전 | 580 | |
| 7620 | 10년 전 | 536 | ||
| 7619 | 10년 전 | 477 | ||
| 7618 | 10년 전 | 1009 | ||
| 7617 | 10년 전 | 720 | ||
| 7616 | 10년 전 | 628 | ||
| 7615 | 10년 전 | 724 | ||
| 7614 | 10년 전 | 1250 | ||
| 7613 |
|
10년 전 | 2079 | |
| 7612 | 10년 전 | 1133 | ||
| 7611 | 10년 전 | 1400 | ||
| 7610 |
|
10년 전 | 1897 | |
| 7609 |
|
10년 전 | 1326 | |
| 7608 |
mwdkim
|
10년 전 | 1120 | |
| 7607 |
|
10년 전 | 1045 | |
| 7606 |
mwdkim
|
10년 전 | 3915 | |
| 7605 | 10년 전 | 685 | ||
| 7604 | 10년 전 | 1021 | ||
| 7603 | 10년 전 | 1643 | ||
| 7602 |
|
10년 전 | 1061 | |
| 7601 |
AniNest
|
10년 전 | 2780 | |
| 7600 |
port443
|
10년 전 | 1018 | |
| 7599 | 10년 전 | 939 | ||
| 7598 | 10년 전 | 1010 | ||
| 7597 | 10년 전 | 4566 | ||
| 7596 |
SeungYeon
|
10년 전 | 884 | |
| 7595 |
untitled
|
10년 전 | 2411 | |
| 7594 |
프로그래머7
|
10년 전 | 1716 | |
| 7593 |
untitled
|
10년 전 | 2356 | |
| 7592 |
untitled
|
10년 전 | 1929 | |
| 7591 |
untitled
|
10년 전 | 2673 | |
| 7590 |
아리마2001
|
10년 전 | 848 | |
| 7589 | 10년 전 | 1101 | ||
| 7588 |
|
10년 전 | 2910 | |
| 7587 | 10년 전 | 1300 | ||
| 7586 | 10년 전 | 662 | ||
| 7585 | 10년 전 | 1684 | ||
| 7584 | 10년 전 | 1404 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1157 | |
| 7582 |
|
10년 전 | 1097 | |
| 7581 | 10년 전 | 1317 | ||
| 7580 | 10년 전 | 979 | ||
| 7579 |
|
10년 전 | 601 | |
| 7578 | 10년 전 | 1425 | ||
| 7577 |
|
10년 전 | 1866 | |
| 7576 | 10년 전 | 1381 | ||
| 7575 |
멋진남자임
|
10년 전 | 1458 | |
| 7574 | 10년 전 | 2111 | ||
| 7573 | 10년 전 | 3244 | ||
| 7572 | 10년 전 | 756 | ||
| 7571 |
|
10년 전 | 778 | |
| 7570 |
|
10년 전 | 1302 | |
| 7569 | 10년 전 | 1544 | ||
| 7568 |
this1mg
|
10년 전 | 1044 | |
| 7567 |
|
10년 전 | 753 | |
| 7566 | 10년 전 | 913 | ||
| 7565 |
Angel하늘
|
10년 전 | 987 | |
| 7564 |
seoldi
|
10년 전 | 1225 | |
| 7563 |
|
10년 전 | 1363 | |
| 7562 |
멋진남자임
|
10년 전 | 2074 | |
| 7561 | 10년 전 | 703 | ||
| 7560 |
leeleeleelee
|
10년 전 | 892 | |
| 7559 | 10년 전 | 5032 | ||
| 7558 |
RinaP
|
10년 전 | 771 | |
| 7557 |
|
10년 전 | 1233 | |
| 7556 | 10년 전 | 1185 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1654 | |
| 7554 | 10년 전 | 1088 | ||
| 7553 |
senseme
|
10년 전 | 1332 | |
| 7552 |
ehdltdoit
|
10년 전 | 1430 | |
| 7551 |
|
10년 전 | 1814 | |
| 7550 |
leeleeleelee
|
10년 전 | 1582 | |
| 7549 | 10년 전 | 2415 | ||
| 7548 | 10년 전 | 1834 | ||
| 7547 |
멋진남자임
|
10년 전 | 1956 | |
| 7546 | 10년 전 | 998 | ||
| 7545 |
ILMare1003
|
10년 전 | 1275 | |
| 7544 |
|
10년 전 | 1240 | |
| 7543 | 10년 전 | 879 | ||
| 7542 | 10년 전 | 651 | ||
| 7541 |
울라라라우
|
10년 전 | 859 | |
| 7540 | 10년 전 | 1595 | ||
| 7539 | 10년 전 | 924 | ||
| 7538 |
|
10년 전 | 1826 | |
| 7537 | 10년 전 | 3605 | ||
| 7536 |
Gaumi
|
10년 전 | 1404 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1259 | |
| 7534 |
senseme
|
10년 전 | 1201 | |
| 7533 | 10년 전 | 1188 | ||
| 7532 | 10년 전 | 852 | ||
| 7531 | 10년 전 | 2043 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기