유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6130 |
gender
|
12년 전 | 1083 | |
| 6129 | 12년 전 | 1320 | ||
| 6128 |
|
12년 전 | 3461 | |
| 6127 | 12년 전 | 694 | ||
| 6126 |
|
12년 전 | 2205 | |
| 6125 |
|
12년 전 | 4881 | |
| 6124 | 12년 전 | 656 | ||
| 6123 | 12년 전 | 3838 | ||
| 6122 | 12년 전 | 1016 | ||
| 6121 | 12년 전 | 3749 | ||
| 6120 | 12년 전 | 906 | ||
| 6119 | 12년 전 | 1767 | ||
| 6118 | 12년 전 | 928 | ||
| 6117 | 12년 전 | 2147 | ||
| 6116 | 12년 전 | 7937 | ||
| 6115 | 12년 전 | 1916 | ||
| 6114 |
|
12년 전 | 1703 | |
| 6113 | 12년 전 | 1520 | ||
| 6112 | 12년 전 | 589 | ||
| 6111 | 12년 전 | 2106 | ||
| 6110 | 12년 전 | 1848 | ||
| 6109 | 12년 전 | 637 | ||
| 6108 | 12년 전 | 1197 | ||
| 6107 | 12년 전 | 617 | ||
| 6106 | 12년 전 | 900 | ||
| 6105 | 12년 전 | 1187 | ||
| 6104 | 12년 전 | 3586 | ||
| 6103 | 12년 전 | 2116 | ||
| 6102 | 12년 전 | 2308 | ||
| 6101 | 12년 전 | 3668 | ||
| 6100 | 12년 전 | 3479 | ||
| 6099 | 12년 전 | 3147 | ||
| 6098 | 12년 전 | 4021 | ||
| 6097 | 12년 전 | 1009 | ||
| 6096 | 12년 전 | 5977 | ||
| 6095 | 12년 전 | 1367 | ||
| 6094 | 12년 전 | 1222 | ||
| 6093 | 12년 전 | 3401 | ||
| 6092 | 12년 전 | 3043 | ||
| 6091 | 12년 전 | 5187 | ||
| 6090 | 12년 전 | 2697 | ||
| 6089 | 12년 전 | 3325 | ||
| 6088 | 12년 전 | 1013 | ||
| 6087 | 12년 전 | 843 | ||
| 6086 | 12년 전 | 2016 | ||
| 6085 |
|
12년 전 | 806 | |
| 6084 |
웹디자인되고파
|
12년 전 | 2214 | |
| 6083 | 12년 전 | 1504 | ||
| 6082 | 12년 전 | 1105 | ||
| 6081 | 12년 전 | 2096 | ||
| 6080 |
Stiven
|
12년 전 | 2309 | |
| 6079 |
프로프리랜서
|
12년 전 | 1313 | |
| 6078 |
프로프리랜서
|
12년 전 | 786 | |
| 6077 |
프로프리랜서
|
12년 전 | 1339 | |
| 6076 |
프로프리랜서
|
12년 전 | 825 | |
| 6075 |
프로프리랜서
|
12년 전 | 1215 | |
| 6074 | 12년 전 | 3852 | ||
| 6073 | 12년 전 | 3947 | ||
| 6072 | 12년 전 | 1372 | ||
| 6071 | 12년 전 | 6984 | ||
| 6070 | 12년 전 | 7567 | ||
| 6069 | 12년 전 | 2314 | ||
| 6068 | 12년 전 | 3864 | ||
| 6067 |
smwkd
|
12년 전 | 630 | |
| 6066 | 12년 전 | 3654 | ||
| 6065 | 12년 전 | 3480 | ||
| 6064 | 12년 전 | 2715 | ||
| 6063 | 12년 전 | 2829 | ||
| 6062 | 12년 전 | 2363 | ||
| 6061 | 12년 전 | 2269 | ||
| 6060 | 12년 전 | 5178 | ||
| 6059 | 12년 전 | 2805 | ||
| 6058 | 12년 전 | 3130 | ||
| 6057 | 12년 전 | 2256 | ||
| 6056 | 12년 전 | 6838 | ||
| 6055 | 12년 전 | 2576 | ||
| 6054 | 12년 전 | 3429 | ||
| 6053 | 12년 전 | 2319 | ||
| 6052 | 12년 전 | 4816 | ||
| 6051 | 12년 전 | 3702 | ||
| 6050 | 12년 전 | 2551 | ||
| 6049 | 12년 전 | 2235 | ||
| 6048 |
|
12년 전 | 1288 | |
| 6047 | 12년 전 | 3432 | ||
| 6046 | 12년 전 | 4088 | ||
| 6045 | 12년 전 | 3453 | ||
| 6044 | 12년 전 | 5329 | ||
| 6043 | 12년 전 | 1648 | ||
| 6042 | 12년 전 | 1277 | ||
| 6041 | 12년 전 | 5163 | ||
| 6040 | 12년 전 | 936 | ||
| 6039 | 12년 전 | 3405 | ||
| 6038 | 12년 전 | 3405 | ||
| 6037 | 12년 전 | 2991 | ||
| 6036 | 12년 전 | 3340 | ||
| 6035 | 12년 전 | 2865 | ||
| 6034 | 12년 전 | 2843 | ||
| 6033 | 12년 전 | 2864 | ||
| 6032 | 12년 전 | 2854 | ||
| 6031 | 12년 전 | 2877 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기