유용한 함수 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년 전 | 4011 | ||
| 129 | 20년 전 | 2949 | ||
| 128 | 20년 전 | 3687 | ||
| 127 | 20년 전 | 3524 | ||
| 126 | 20년 전 | 3775 | ||
| 125 | 20년 전 | 8596 | ||
| 124 | 20년 전 | 2604 | ||
| 123 | 20년 전 | 3755 | ||
| 122 | 20년 전 | 3213 | ||
| 121 | 20년 전 | 2615 | ||
| 120 | 20년 전 | 2675 | ||
| 119 | 20년 전 | 2589 | ||
| 118 | 20년 전 | 2864 | ||
| 117 |
|
20년 전 | 3064 | |
| 116 | 20년 전 | 5324 | ||
| 115 | 20년 전 | 3932 | ||
| 114 | 20년 전 | 4980 | ||
| 113 | 20년 전 | 6220 | ||
| 112 | 20년 전 | 7326 | ||
| 111 | 20년 전 | 18436 | ||
| 110 | 20년 전 | 6881 | ||
| 109 | 20년 전 | 2889 | ||
| 108 | 20년 전 | 4145 | ||
| 107 |
prosper
|
20년 전 | 2505 | |
| 106 |
prosper
|
20년 전 | 4323 | |
| 105 |
아우겐나이스
|
20년 전 | 2915 | |
| 104 | 20년 전 | 2266 | ||
| 103 | 20년 전 | 2482 | ||
| 102 | 20년 전 | 2266 | ||
| 101 | 20년 전 | 2581 | ||
| 100 | 20년 전 | 1757 | ||
| 99 | 20년 전 | 1575 | ||
| 98 | 20년 전 | 1620 | ||
| 97 | 20년 전 | 2134 | ||
| 96 | 20년 전 | 1886 | ||
| 95 | 20년 전 | 2383 | ||
| 94 | 20년 전 | 3568 | ||
| 93 | 20년 전 | 1568 | ||
| 92 | 20년 전 | 1765 | ||
| 91 | 20년 전 | 3187 | ||
| 90 | 21년 전 | 2346 | ||
| 89 | 21년 전 | 3182 | ||
| 88 | 21년 전 | 2873 | ||
| 87 | 21년 전 | 3300 | ||
| 86 | 21년 전 | 5142 | ||
| 85 | 21년 전 | 2528 | ||
| 84 | 21년 전 | 4832 | ||
| 83 | 21년 전 | 2510 | ||
| 82 | 21년 전 | 3128 | ||
| 81 | 21년 전 | 7636 | ||
| 80 | 21년 전 | 3836 | ||
| 79 | 21년 전 | 3216 | ||
| 78 | 21년 전 | 4700 | ||
| 77 | 21년 전 | 2909 | ||
| 76 | 21년 전 | 6226 | ||
| 75 | 21년 전 | 4475 | ||
| 74 | 21년 전 | 5788 | ||
| 73 | 21년 전 | 3632 | ||
| 72 | 21년 전 | 5975 | ||
| 71 | 21년 전 | 3121 | ||
| 70 | 21년 전 | 2849 | ||
| 69 | 21년 전 | 2646 | ||
| 68 | 21년 전 | 2451 | ||
| 67 | 21년 전 | 2660 | ||
| 66 | 21년 전 | 2681 | ||
| 65 | 21년 전 | 3794 | ||
| 64 | 21년 전 | 2834 | ||
| 63 | 21년 전 | 2463 | ||
| 62 | 21년 전 | 2273 | ||
| 61 | 21년 전 | 3084 | ||
| 60 | 21년 전 | 3137 | ||
| 59 | 21년 전 | 2523 | ||
| 58 | 21년 전 | 2594 | ||
| 57 | 21년 전 | 2974 | ||
| 56 | 21년 전 | 2337 | ||
| 55 | 21년 전 | 2765 | ||
| 54 | 21년 전 | 2133 | ||
| 53 | 21년 전 | 2360 | ||
| 52 | 21년 전 | 2705 | ||
| 51 |
prosper
|
21년 전 | 2355 | |
| 50 |
prosper
|
21년 전 | 2178 | |
| 49 | 21년 전 | 2184 | ||
| 48 | 21년 전 | 2345 | ||
| 47 | 21년 전 | 1937 | ||
| 46 | 21년 전 | 1934 | ||
| 45 | 21년 전 | 2137 | ||
| 44 | 21년 전 | 2368 | ||
| 43 | 21년 전 | 4585 | ||
| 42 |
prosper
|
21년 전 | 2718 | |
| 41 |
prosper
|
21년 전 | 2117 | |
| 40 | 21년 전 | 2185 | ||
| 39 | 21년 전 | 2154 | ||
| 38 | 21년 전 | 2426 | ||
| 37 | 21년 전 | 2573 | ||
| 36 | 21년 전 | 1782 | ||
| 35 | 21년 전 | 4079 | ||
| 34 | 21년 전 | 3855 | ||
| 33 | 21년 전 | 2995 | ||
| 32 |
prosper
|
21년 전 | 2908 | |
| 31 | 21년 전 | 5290 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기