유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 130 | 20년 전 | 3989 | ||
| 129 | 20년 전 | 2930 | ||
| 128 | 20년 전 | 3667 | ||
| 127 | 20년 전 | 3494 | ||
| 126 | 20년 전 | 3754 | ||
| 125 | 20년 전 | 8583 | ||
| 124 | 20년 전 | 2599 | ||
| 123 | 20년 전 | 3743 | ||
| 122 | 20년 전 | 3204 | ||
| 121 | 20년 전 | 2608 | ||
| 120 | 20년 전 | 2663 | ||
| 119 | 20년 전 | 2579 | ||
| 118 | 20년 전 | 2859 | ||
| 117 |
|
20년 전 | 3051 | |
| 116 | 20년 전 | 5304 | ||
| 115 | 20년 전 | 3918 | ||
| 114 | 20년 전 | 4965 | ||
| 113 | 20년 전 | 6206 | ||
| 112 | 20년 전 | 7322 | ||
| 111 | 20년 전 | 18427 | ||
| 110 | 20년 전 | 6870 | ||
| 109 | 20년 전 | 2872 | ||
| 108 | 20년 전 | 4136 | ||
| 107 |
prosper
|
20년 전 | 2491 | |
| 106 |
prosper
|
20년 전 | 4314 | |
| 105 |
아우겐나이스
|
20년 전 | 2908 | |
| 104 | 20년 전 | 2255 | ||
| 103 | 20년 전 | 2473 | ||
| 102 | 20년 전 | 2248 | ||
| 101 | 20년 전 | 2570 | ||
| 100 | 20년 전 | 1742 | ||
| 99 | 20년 전 | 1562 | ||
| 98 | 20년 전 | 1615 | ||
| 97 | 20년 전 | 2126 | ||
| 96 | 20년 전 | 1870 | ||
| 95 | 20년 전 | 2369 | ||
| 94 | 20년 전 | 3563 | ||
| 93 | 20년 전 | 1557 | ||
| 92 | 20년 전 | 1753 | ||
| 91 | 20년 전 | 3177 | ||
| 90 | 20년 전 | 2339 | ||
| 89 | 20년 전 | 3167 | ||
| 88 | 20년 전 | 2868 | ||
| 87 | 20년 전 | 3293 | ||
| 86 | 20년 전 | 5127 | ||
| 85 | 20년 전 | 2516 | ||
| 84 | 20년 전 | 4823 | ||
| 83 | 20년 전 | 2500 | ||
| 82 | 20년 전 | 3119 | ||
| 81 | 20년 전 | 7618 | ||
| 80 | 20년 전 | 3819 | ||
| 79 | 20년 전 | 3205 | ||
| 78 | 20년 전 | 4691 | ||
| 77 | 20년 전 | 2893 | ||
| 76 | 20년 전 | 6214 | ||
| 75 | 20년 전 | 4470 | ||
| 74 | 20년 전 | 5777 | ||
| 73 | 20년 전 | 3622 | ||
| 72 | 20년 전 | 5964 | ||
| 71 | 20년 전 | 3110 | ||
| 70 | 20년 전 | 2834 | ||
| 69 | 20년 전 | 2636 | ||
| 68 | 20년 전 | 2443 | ||
| 67 | 20년 전 | 2649 | ||
| 66 | 20년 전 | 2666 | ||
| 65 | 20년 전 | 3783 | ||
| 64 | 20년 전 | 2821 | ||
| 63 | 20년 전 | 2452 | ||
| 62 | 20년 전 | 2264 | ||
| 61 | 20년 전 | 3067 | ||
| 60 | 20년 전 | 3131 | ||
| 59 | 20년 전 | 2511 | ||
| 58 | 20년 전 | 2587 | ||
| 57 | 20년 전 | 2962 | ||
| 56 | 20년 전 | 2315 | ||
| 55 | 20년 전 | 2758 | ||
| 54 | 20년 전 | 2125 | ||
| 53 | 20년 전 | 2348 | ||
| 52 | 20년 전 | 2696 | ||
| 51 |
prosper
|
21년 전 | 2348 | |
| 50 |
prosper
|
21년 전 | 2166 | |
| 49 | 21년 전 | 2174 | ||
| 48 | 21년 전 | 2333 | ||
| 47 | 21년 전 | 1929 | ||
| 46 | 21년 전 | 1927 | ||
| 45 | 21년 전 | 2130 | ||
| 44 | 21년 전 | 2357 | ||
| 43 | 21년 전 | 4568 | ||
| 42 |
prosper
|
21년 전 | 2712 | |
| 41 |
prosper
|
21년 전 | 2110 | |
| 40 | 21년 전 | 2178 | ||
| 39 | 21년 전 | 2141 | ||
| 38 | 21년 전 | 2416 | ||
| 37 | 21년 전 | 2566 | ||
| 36 | 21년 전 | 1769 | ||
| 35 | 21년 전 | 4069 | ||
| 34 | 21년 전 | 3849 | ||
| 33 | 21년 전 | 2989 | ||
| 32 |
prosper
|
21년 전 | 2899 | |
| 31 | 21년 전 | 5274 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기