유용한 함수 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년 전 | 4004 | ||
| 129 | 20년 전 | 2942 | ||
| 128 | 20년 전 | 3675 | ||
| 127 | 20년 전 | 3512 | ||
| 126 | 20년 전 | 3765 | ||
| 125 | 20년 전 | 8594 | ||
| 124 | 20년 전 | 2602 | ||
| 123 | 20년 전 | 3750 | ||
| 122 | 20년 전 | 3209 | ||
| 121 | 20년 전 | 2611 | ||
| 120 | 20년 전 | 2673 | ||
| 119 | 20년 전 | 2586 | ||
| 118 | 20년 전 | 2861 | ||
| 117 |
|
20년 전 | 3062 | |
| 116 | 20년 전 | 5314 | ||
| 115 | 20년 전 | 3924 | ||
| 114 | 20년 전 | 4973 | ||
| 113 | 20년 전 | 6212 | ||
| 112 | 20년 전 | 7325 | ||
| 111 | 20년 전 | 18431 | ||
| 110 | 20년 전 | 6875 | ||
| 109 | 20년 전 | 2885 | ||
| 108 | 20년 전 | 4141 | ||
| 107 |
prosper
|
20년 전 | 2501 | |
| 106 |
prosper
|
20년 전 | 4319 | |
| 105 |
아우겐나이스
|
20년 전 | 2913 | |
| 104 | 20년 전 | 2260 | ||
| 103 | 20년 전 | 2478 | ||
| 102 | 20년 전 | 2261 | ||
| 101 | 20년 전 | 2576 | ||
| 100 | 20년 전 | 1750 | ||
| 99 | 20년 전 | 1571 | ||
| 98 | 20년 전 | 1618 | ||
| 97 | 20년 전 | 2131 | ||
| 96 | 20년 전 | 1878 | ||
| 95 | 20년 전 | 2378 | ||
| 94 | 20년 전 | 3567 | ||
| 93 | 20년 전 | 1565 | ||
| 92 | 20년 전 | 1757 | ||
| 91 | 20년 전 | 3183 | ||
| 90 | 20년 전 | 2343 | ||
| 89 | 20년 전 | 3171 | ||
| 88 | 20년 전 | 2872 | ||
| 87 | 20년 전 | 3299 | ||
| 86 | 20년 전 | 5136 | ||
| 85 | 20년 전 | 2522 | ||
| 84 | 20년 전 | 4825 | ||
| 83 | 20년 전 | 2507 | ||
| 82 | 20년 전 | 3124 | ||
| 81 | 20년 전 | 7631 | ||
| 80 | 20년 전 | 3825 | ||
| 79 | 20년 전 | 3211 | ||
| 78 | 20년 전 | 4697 | ||
| 77 | 20년 전 | 2902 | ||
| 76 | 20년 전 | 6221 | ||
| 75 | 20년 전 | 4471 | ||
| 74 | 20년 전 | 5783 | ||
| 73 | 20년 전 | 3627 | ||
| 72 | 20년 전 | 5971 | ||
| 71 | 20년 전 | 3119 | ||
| 70 | 20년 전 | 2845 | ||
| 69 | 21년 전 | 2642 | ||
| 68 | 21년 전 | 2448 | ||
| 67 | 21년 전 | 2658 | ||
| 66 | 21년 전 | 2673 | ||
| 65 | 21년 전 | 3791 | ||
| 64 | 21년 전 | 2828 | ||
| 63 | 21년 전 | 2457 | ||
| 62 | 21년 전 | 2266 | ||
| 61 | 21년 전 | 3079 | ||
| 60 | 21년 전 | 3134 | ||
| 59 | 21년 전 | 2520 | ||
| 58 | 21년 전 | 2593 | ||
| 57 | 21년 전 | 2968 | ||
| 56 | 21년 전 | 2322 | ||
| 55 | 21년 전 | 2763 | ||
| 54 | 21년 전 | 2129 | ||
| 53 | 21년 전 | 2355 | ||
| 52 | 21년 전 | 2700 | ||
| 51 |
prosper
|
21년 전 | 2351 | |
| 50 |
prosper
|
21년 전 | 2172 | |
| 49 | 21년 전 | 2180 | ||
| 48 | 21년 전 | 2344 | ||
| 47 | 21년 전 | 1935 | ||
| 46 | 21년 전 | 1931 | ||
| 45 | 21년 전 | 2136 | ||
| 44 | 21년 전 | 2362 | ||
| 43 | 21년 전 | 4583 | ||
| 42 |
prosper
|
21년 전 | 2716 | |
| 41 |
prosper
|
21년 전 | 2115 | |
| 40 | 21년 전 | 2180 | ||
| 39 | 21년 전 | 2150 | ||
| 38 | 21년 전 | 2421 | ||
| 37 | 21년 전 | 2570 | ||
| 36 | 21년 전 | 1777 | ||
| 35 | 21년 전 | 4073 | ||
| 34 | 21년 전 | 3851 | ||
| 33 | 21년 전 | 2990 | ||
| 32 |
prosper
|
21년 전 | 2905 | |
| 31 | 21년 전 | 5286 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기