유용한 함수 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년 전 | 652 | ||
| 7629 |
|
10년 전 | 2389 | |
| 7628 | 10년 전 | 792 | ||
| 7627 |
|
10년 전 | 1031 | |
| 7626 |
|
10년 전 | 1788 | |
| 7625 | 10년 전 | 709 | ||
| 7624 | 10년 전 | 725 | ||
| 7623 |
|
10년 전 | 3092 | |
| 7622 | 10년 전 | 727 | ||
| 7621 |
leeleeleelee
|
10년 전 | 586 | |
| 7620 | 10년 전 | 540 | ||
| 7619 | 10년 전 | 488 | ||
| 7618 | 10년 전 | 1024 | ||
| 7617 | 10년 전 | 726 | ||
| 7616 | 10년 전 | 650 | ||
| 7615 | 10년 전 | 726 | ||
| 7614 | 10년 전 | 1264 | ||
| 7613 |
|
10년 전 | 2085 | |
| 7612 | 10년 전 | 1144 | ||
| 7611 | 10년 전 | 1417 | ||
| 7610 |
|
10년 전 | 1903 | |
| 7609 |
|
10년 전 | 1344 | |
| 7608 |
mwdkim
|
10년 전 | 1131 | |
| 7607 |
|
10년 전 | 1053 | |
| 7606 |
mwdkim
|
10년 전 | 3927 | |
| 7605 | 10년 전 | 691 | ||
| 7604 | 10년 전 | 1027 | ||
| 7603 | 10년 전 | 1653 | ||
| 7602 |
|
10년 전 | 1078 | |
| 7601 |
AniNest
|
10년 전 | 2792 | |
| 7600 |
port443
|
10년 전 | 1031 | |
| 7599 | 10년 전 | 945 | ||
| 7598 | 10년 전 | 1020 | ||
| 7597 | 10년 전 | 4575 | ||
| 7596 |
SeungYeon
|
10년 전 | 890 | |
| 7595 |
untitled
|
10년 전 | 2425 | |
| 7594 |
프로그래머7
|
10년 전 | 1728 | |
| 7593 |
untitled
|
10년 전 | 2365 | |
| 7592 |
untitled
|
10년 전 | 1942 | |
| 7591 |
untitled
|
10년 전 | 2676 | |
| 7590 |
아리마2001
|
10년 전 | 854 | |
| 7589 | 10년 전 | 1111 | ||
| 7588 |
|
10년 전 | 2919 | |
| 7587 | 10년 전 | 1303 | ||
| 7586 | 10년 전 | 672 | ||
| 7585 | 10년 전 | 1694 | ||
| 7584 | 10년 전 | 1414 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1163 | |
| 7582 |
|
10년 전 | 1114 | |
| 7581 | 10년 전 | 1347 | ||
| 7580 | 10년 전 | 995 | ||
| 7579 |
|
10년 전 | 609 | |
| 7578 | 10년 전 | 1430 | ||
| 7577 |
|
10년 전 | 1872 | |
| 7576 | 10년 전 | 1385 | ||
| 7575 |
멋진남자임
|
10년 전 | 1464 | |
| 7574 | 10년 전 | 2123 | ||
| 7573 | 10년 전 | 3257 | ||
| 7572 | 10년 전 | 761 | ||
| 7571 |
|
10년 전 | 783 | |
| 7570 |
|
10년 전 | 1315 | |
| 7569 | 10년 전 | 1552 | ||
| 7568 |
this1mg
|
10년 전 | 1051 | |
| 7567 |
|
10년 전 | 762 | |
| 7566 | 10년 전 | 918 | ||
| 7565 |
Angel하늘
|
10년 전 | 1005 | |
| 7564 |
seoldi
|
10년 전 | 1239 | |
| 7563 |
|
10년 전 | 1383 | |
| 7562 |
멋진남자임
|
10년 전 | 2086 | |
| 7561 | 10년 전 | 714 | ||
| 7560 |
leeleeleelee
|
10년 전 | 904 | |
| 7559 | 10년 전 | 5047 | ||
| 7558 |
RinaP
|
10년 전 | 785 | |
| 7557 |
|
10년 전 | 1246 | |
| 7556 | 10년 전 | 1194 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1665 | |
| 7554 | 10년 전 | 1094 | ||
| 7553 |
senseme
|
10년 전 | 1339 | |
| 7552 |
ehdltdoit
|
10년 전 | 1437 | |
| 7551 |
|
10년 전 | 1825 | |
| 7550 |
leeleeleelee
|
10년 전 | 1591 | |
| 7549 | 10년 전 | 2425 | ||
| 7548 | 10년 전 | 1843 | ||
| 7547 |
멋진남자임
|
10년 전 | 1963 | |
| 7546 | 10년 전 | 1004 | ||
| 7545 |
ILMare1003
|
10년 전 | 1288 | |
| 7544 |
|
10년 전 | 1252 | |
| 7543 | 10년 전 | 884 | ||
| 7542 | 10년 전 | 657 | ||
| 7541 |
울라라라우
|
10년 전 | 865 | |
| 7540 | 10년 전 | 1600 | ||
| 7539 | 10년 전 | 936 | ||
| 7538 |
|
10년 전 | 1834 | |
| 7537 | 10년 전 | 3611 | ||
| 7536 |
Gaumi
|
10년 전 | 1416 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1274 | |
| 7534 |
senseme
|
10년 전 | 1207 | |
| 7533 | 10년 전 | 1197 | ||
| 7532 | 10년 전 | 859 | ||
| 7531 | 10년 전 | 2055 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기