유용한 함수 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년 전 | 671 | ||
| 7629 |
|
10년 전 | 2422 | |
| 7628 | 10년 전 | 808 | ||
| 7627 |
|
10년 전 | 1045 | |
| 7626 |
|
10년 전 | 1802 | |
| 7625 | 10년 전 | 732 | ||
| 7624 | 10년 전 | 747 | ||
| 7623 |
|
10년 전 | 3123 | |
| 7622 | 10년 전 | 747 | ||
| 7621 |
leeleeleelee
|
10년 전 | 602 | |
| 7620 | 10년 전 | 550 | ||
| 7619 | 10년 전 | 513 | ||
| 7618 | 10년 전 | 1050 | ||
| 7617 | 10년 전 | 736 | ||
| 7616 | 10년 전 | 671 | ||
| 7615 | 10년 전 | 738 | ||
| 7614 | 10년 전 | 1280 | ||
| 7613 |
|
10년 전 | 2094 | |
| 7612 | 10년 전 | 1177 | ||
| 7611 | 10년 전 | 1438 | ||
| 7610 |
|
10년 전 | 1916 | |
| 7609 |
|
10년 전 | 1371 | |
| 7608 |
mwdkim
|
10년 전 | 1147 | |
| 7607 |
|
10년 전 | 1078 | |
| 7606 |
mwdkim
|
10년 전 | 3954 | |
| 7605 | 10년 전 | 703 | ||
| 7604 | 10년 전 | 1040 | ||
| 7603 | 10년 전 | 1663 | ||
| 7602 |
|
10년 전 | 1094 | |
| 7601 |
AniNest
|
10년 전 | 2810 | |
| 7600 |
port443
|
10년 전 | 1050 | |
| 7599 | 10년 전 | 957 | ||
| 7598 | 10년 전 | 1037 | ||
| 7597 | 10년 전 | 4586 | ||
| 7596 |
SeungYeon
|
10년 전 | 908 | |
| 7595 |
untitled
|
10년 전 | 2450 | |
| 7594 |
프로그래머7
|
10년 전 | 1747 | |
| 7593 |
untitled
|
10년 전 | 2387 | |
| 7592 |
untitled
|
10년 전 | 1957 | |
| 7591 |
untitled
|
10년 전 | 2692 | |
| 7590 |
아리마2001
|
10년 전 | 868 | |
| 7589 | 10년 전 | 1120 | ||
| 7588 |
|
10년 전 | 2938 | |
| 7587 | 10년 전 | 1319 | ||
| 7586 | 10년 전 | 684 | ||
| 7585 | 10년 전 | 1720 | ||
| 7584 | 10년 전 | 1423 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1187 | |
| 7582 |
|
10년 전 | 1148 | |
| 7581 | 10년 전 | 1375 | ||
| 7580 | 10년 전 | 1027 | ||
| 7579 |
|
10년 전 | 618 | |
| 7578 | 10년 전 | 1440 | ||
| 7577 |
|
10년 전 | 1887 | |
| 7576 | 10년 전 | 1399 | ||
| 7575 |
멋진남자임
|
10년 전 | 1481 | |
| 7574 | 10년 전 | 2139 | ||
| 7573 | 10년 전 | 3276 | ||
| 7572 | 10년 전 | 776 | ||
| 7571 |
|
10년 전 | 796 | |
| 7570 |
|
10년 전 | 1337 | |
| 7569 | 10년 전 | 1565 | ||
| 7568 |
this1mg
|
10년 전 | 1063 | |
| 7567 |
|
10년 전 | 780 | |
| 7566 | 10년 전 | 929 | ||
| 7565 |
Angel하늘
|
10년 전 | 1030 | |
| 7564 |
seoldi
|
10년 전 | 1274 | |
| 7563 |
|
10년 전 | 1414 | |
| 7562 |
멋진남자임
|
10년 전 | 2112 | |
| 7561 | 10년 전 | 731 | ||
| 7560 |
leeleeleelee
|
10년 전 | 928 | |
| 7559 | 10년 전 | 5068 | ||
| 7558 |
RinaP
|
10년 전 | 808 | |
| 7557 |
|
10년 전 | 1270 | |
| 7556 | 10년 전 | 1209 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1679 | |
| 7554 | 10년 전 | 1107 | ||
| 7553 |
senseme
|
10년 전 | 1355 | |
| 7552 |
ehdltdoit
|
10년 전 | 1452 | |
| 7551 |
|
10년 전 | 1846 | |
| 7550 |
leeleeleelee
|
10년 전 | 1614 | |
| 7549 | 10년 전 | 2447 | ||
| 7548 | 10년 전 | 1856 | ||
| 7547 |
멋진남자임
|
10년 전 | 1974 | |
| 7546 | 10년 전 | 1020 | ||
| 7545 |
ILMare1003
|
10년 전 | 1311 | |
| 7544 |
|
10년 전 | 1265 | |
| 7543 | 10년 전 | 901 | ||
| 7542 | 10년 전 | 681 | ||
| 7541 |
울라라라우
|
10년 전 | 878 | |
| 7540 | 10년 전 | 1608 | ||
| 7539 | 10년 전 | 953 | ||
| 7538 |
|
10년 전 | 1843 | |
| 7537 | 10년 전 | 3628 | ||
| 7536 |
Gaumi
|
10년 전 | 1435 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1292 | |
| 7534 |
senseme
|
10년 전 | 1217 | |
| 7533 | 10년 전 | 1221 | ||
| 7532 | 10년 전 | 884 | ||
| 7531 | 10년 전 | 2074 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기