유용한 함수 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년 전 | 635 | ||
| 7629 |
|
10년 전 | 2353 | |
| 7628 | 10년 전 | 771 | ||
| 7627 |
|
10년 전 | 1005 | |
| 7626 |
|
10년 전 | 1761 | |
| 7625 | 10년 전 | 665 | ||
| 7624 | 10년 전 | 686 | ||
| 7623 |
|
10년 전 | 3022 | |
| 7622 | 10년 전 | 701 | ||
| 7621 |
leeleeleelee
|
10년 전 | 572 | |
| 7620 | 10년 전 | 530 | ||
| 7619 | 10년 전 | 464 | ||
| 7618 | 10년 전 | 1002 | ||
| 7617 | 10년 전 | 714 | ||
| 7616 | 10년 전 | 618 | ||
| 7615 | 10년 전 | 713 | ||
| 7614 | 10년 전 | 1223 | ||
| 7613 |
|
10년 전 | 2060 | |
| 7612 | 10년 전 | 1125 | ||
| 7611 | 10년 전 | 1385 | ||
| 7610 |
|
10년 전 | 1882 | |
| 7609 |
|
10년 전 | 1295 | |
| 7608 |
mwdkim
|
10년 전 | 1100 | |
| 7607 |
|
10년 전 | 1027 | |
| 7606 |
mwdkim
|
10년 전 | 3905 | |
| 7605 | 10년 전 | 672 | ||
| 7604 | 10년 전 | 1009 | ||
| 7603 | 10년 전 | 1634 | ||
| 7602 |
|
10년 전 | 1043 | |
| 7601 |
AniNest
|
10년 전 | 2765 | |
| 7600 |
port443
|
10년 전 | 1002 | |
| 7599 | 10년 전 | 929 | ||
| 7598 | 10년 전 | 995 | ||
| 7597 | 10년 전 | 4559 | ||
| 7596 |
SeungYeon
|
10년 전 | 871 | |
| 7595 |
untitled
|
10년 전 | 2397 | |
| 7594 |
프로그래머7
|
10년 전 | 1700 | |
| 7593 |
untitled
|
10년 전 | 2342 | |
| 7592 |
untitled
|
10년 전 | 1920 | |
| 7591 |
untitled
|
10년 전 | 2659 | |
| 7590 |
아리마2001
|
10년 전 | 830 | |
| 7589 | 10년 전 | 1088 | ||
| 7588 |
|
10년 전 | 2902 | |
| 7587 | 10년 전 | 1283 | ||
| 7586 | 10년 전 | 647 | ||
| 7585 | 10년 전 | 1664 | ||
| 7584 | 10년 전 | 1399 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1142 | |
| 7582 |
|
10년 전 | 1077 | |
| 7581 | 10년 전 | 1308 | ||
| 7580 | 10년 전 | 950 | ||
| 7579 |
|
10년 전 | 590 | |
| 7578 | 10년 전 | 1400 | ||
| 7577 |
|
10년 전 | 1857 | |
| 7576 | 10년 전 | 1369 | ||
| 7575 |
멋진남자임
|
10년 전 | 1448 | |
| 7574 | 10년 전 | 2092 | ||
| 7573 | 10년 전 | 3222 | ||
| 7572 | 10년 전 | 750 | ||
| 7571 |
|
10년 전 | 774 | |
| 7570 |
|
10년 전 | 1296 | |
| 7569 | 10년 전 | 1525 | ||
| 7568 |
this1mg
|
10년 전 | 1027 | |
| 7567 |
|
10년 전 | 737 | |
| 7566 | 10년 전 | 905 | ||
| 7565 |
Angel하늘
|
10년 전 | 969 | |
| 7564 |
seoldi
|
10년 전 | 1211 | |
| 7563 |
|
10년 전 | 1354 | |
| 7562 |
멋진남자임
|
10년 전 | 2053 | |
| 7561 | 10년 전 | 684 | ||
| 7560 |
leeleeleelee
|
10년 전 | 885 | |
| 7559 | 10년 전 | 5018 | ||
| 7558 |
RinaP
|
10년 전 | 757 | |
| 7557 |
|
10년 전 | 1223 | |
| 7556 | 10년 전 | 1172 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1640 | |
| 7554 | 10년 전 | 1078 | ||
| 7553 |
senseme
|
10년 전 | 1323 | |
| 7552 |
ehdltdoit
|
10년 전 | 1420 | |
| 7551 |
|
10년 전 | 1806 | |
| 7550 |
leeleeleelee
|
10년 전 | 1569 | |
| 7549 | 10년 전 | 2399 | ||
| 7548 | 10년 전 | 1820 | ||
| 7547 |
멋진남자임
|
10년 전 | 1934 | |
| 7546 | 10년 전 | 977 | ||
| 7545 |
ILMare1003
|
10년 전 | 1259 | |
| 7544 |
|
10년 전 | 1216 | |
| 7543 | 10년 전 | 867 | ||
| 7542 | 10년 전 | 638 | ||
| 7541 |
울라라라우
|
10년 전 | 850 | |
| 7540 | 10년 전 | 1587 | ||
| 7539 | 10년 전 | 909 | ||
| 7538 |
|
10년 전 | 1819 | |
| 7537 | 10년 전 | 3597 | ||
| 7536 |
Gaumi
|
10년 전 | 1390 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1250 | |
| 7534 |
senseme
|
10년 전 | 1196 | |
| 7533 | 10년 전 | 1176 | ||
| 7532 | 10년 전 | 838 | ||
| 7531 | 10년 전 | 2031 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기