유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7730 | 10년 전 | 1312 | ||
| 7729 | 10년 전 | 1154 | ||
| 7728 |
잘살아보자
|
10년 전 | 606 | |
| 7727 |
잘살아보자
|
10년 전 | 500 | |
| 7726 |
잘살아보자
|
10년 전 | 830 | |
| 7725 |
잘살아보자
|
10년 전 | 559 | |
| 7724 |
잘살아보자
|
10년 전 | 473 | |
| 7723 |
잘살아보자
|
10년 전 | 537 | |
| 7722 |
잘살아보자
|
10년 전 | 481 | |
| 7721 |
잘살아보자
|
10년 전 | 511 | |
| 7720 |
잘살아보자
|
10년 전 | 471 | |
| 7719 |
비긴어게인
|
10년 전 | 694 | |
| 7718 |
|
10년 전 | 2540 | |
| 7717 |
잘살아보자
|
10년 전 | 654 | |
| 7716 |
잘살아보자
|
10년 전 | 405 | |
| 7715 |
잘살아보자
|
10년 전 | 434 | |
| 7714 |
잘살아보자
|
10년 전 | 502 | |
| 7713 | 10년 전 | 1789 | ||
| 7712 | 10년 전 | 1718 | ||
| 7711 | 10년 전 | 1104 | ||
| 7710 | 10년 전 | 1406 | ||
| 7709 | 10년 전 | 1523 | ||
| 7708 | 10년 전 | 1463 | ||
| 7707 | 10년 전 | 860 | ||
| 7706 |
별지기천사
|
10년 전 | 572 | |
| 7705 | 10년 전 | 1080 | ||
| 7704 |
ICONdesignstudio
|
10년 전 | 626 | |
| 7703 | 10년 전 | 596 | ||
| 7702 |
|
10년 전 | 730 | |
| 7701 | 10년 전 | 1420 | ||
| 7700 | 10년 전 | 1112 | ||
| 7699 | 10년 전 | 584 | ||
| 7698 | 10년 전 | 1146 | ||
| 7697 | 10년 전 | 5170 | ||
| 7696 | 10년 전 | 661 | ||
| 7695 | 10년 전 | 1690 | ||
| 7694 | 10년 전 | 1065 | ||
| 7693 | 10년 전 | 1560 | ||
| 7692 | 10년 전 | 1301 | ||
| 7691 | 10년 전 | 829 | ||
| 7690 | 10년 전 | 1393 | ||
| 7689 | 10년 전 | 1020 | ||
| 7688 | 10년 전 | 620 | ||
| 7687 |
파랑새1597
|
10년 전 | 600 | |
| 7686 | 10년 전 | 859 | ||
| 7685 | 10년 전 | 1346 | ||
| 7684 | 10년 전 | 802 | ||
| 7683 | 10년 전 | 1103 | ||
| 7682 | 10년 전 | 1028 | ||
| 7681 | 10년 전 | 668 | ||
| 7680 | 10년 전 | 992 | ||
| 7679 | 10년 전 | 511 | ||
| 7678 | 10년 전 | 744 | ||
| 7677 | 10년 전 | 642 | ||
| 7676 |
|
10년 전 | 950 | |
| 7675 |
|
10년 전 | 1180 | |
| 7674 | 10년 전 | 1056 | ||
| 7673 | 10년 전 | 755 | ||
| 7672 | 10년 전 | 1094 | ||
| 7671 | 10년 전 | 894 | ||
| 7670 | 10년 전 | 665 | ||
| 7669 |
mashmellow
|
10년 전 | 1234 | |
| 7668 | 10년 전 | 721 | ||
| 7667 | 10년 전 | 1016 | ||
| 7666 |
senseme
|
10년 전 | 657 | |
| 7665 | 10년 전 | 513 | ||
| 7664 | 10년 전 | 1899 | ||
| 7663 |
mixx애교
|
10년 전 | 985 | |
| 7662 | 10년 전 | 1046 | ||
| 7661 |
hkhkah
|
10년 전 | 795 | |
| 7660 | 10년 전 | 1068 | ||
| 7659 |
커네드커네드
|
10년 전 | 937 | |
| 7658 |
바람돌이팡
|
10년 전 | 679 | |
| 7657 | 10년 전 | 1169 | ||
| 7656 | 10년 전 | 1581 | ||
| 7655 | 10년 전 | 1000 | ||
| 7654 |
개발짜증나
|
10년 전 | 862 | |
| 7653 |
네이비칼라
|
10년 전 | 883 | |
| 7652 |
밥먹고합시다
|
10년 전 | 815 | |
| 7651 |
플라이SINJI
|
10년 전 | 1514 | |
| 7650 |
개발짜증나
|
10년 전 | 1422 | |
| 7649 | 10년 전 | 455 | ||
| 7648 |
이미영ㅇㅇ
|
10년 전 | 869 | |
| 7647 | 10년 전 | 442 | ||
| 7646 | 10년 전 | 814 | ||
| 7645 | 10년 전 | 2326 | ||
| 7644 | 10년 전 | 825 | ||
| 7643 |
|
10년 전 | 2874 | |
| 7642 | 10년 전 | 1517 | ||
| 7641 | 10년 전 | 1144 | ||
| 7640 |
개발짜증나
|
10년 전 | 477 | |
| 7639 |
|
10년 전 | 817 | |
| 7638 |
개발짜증나
|
10년 전 | 1138 | |
| 7637 | 10년 전 | 1558 | ||
| 7636 | 10년 전 | 2917 | ||
| 7635 | 10년 전 | 1699 | ||
| 7634 | 10년 전 | 1888 | ||
| 7633 | 10년 전 | 2345 | ||
| 7632 | 10년 전 | 3956 | ||
| 7631 |
|
10년 전 | 1542 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기