유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 356 | ||
| 8029 | 9년 전 | 287 | ||
| 8028 | 9년 전 | 255 | ||
| 8027 | 9년 전 | 266 | ||
| 8026 | 9년 전 | 310 | ||
| 8025 | 9년 전 | 367 | ||
| 8024 | 9년 전 | 333 | ||
| 8023 | 9년 전 | 380 | ||
| 8022 | 9년 전 | 310 | ||
| 8021 | 9년 전 | 315 | ||
| 8020 | 9년 전 | 308 | ||
| 8019 | 9년 전 | 331 | ||
| 8018 | 9년 전 | 438 | ||
| 8017 | 9년 전 | 531 | ||
| 8016 | 9년 전 | 317 | ||
| 8015 | 9년 전 | 381 | ||
| 8014 | 9년 전 | 314 | ||
| 8013 | 9년 전 | 227 | ||
| 8012 | 9년 전 | 237 | ||
| 8011 | 9년 전 | 436 | ||
| 8010 | 9년 전 | 290 | ||
| 8009 | 9년 전 | 273 | ||
| 8008 | 9년 전 | 278 | ||
| 8007 | 9년 전 | 420 | ||
| 8006 | 9년 전 | 454 | ||
| 8005 |
|
9년 전 | 957 | |
| 8004 | 9년 전 | 342 | ||
| 8003 | 9년 전 | 413 | ||
| 8002 | 9년 전 | 317 | ||
| 8001 |
|
9년 전 | 661 | |
| 8000 | 9년 전 | 405 | ||
| 7999 | 9년 전 | 366 | ||
| 7998 | 9년 전 | 418 | ||
| 7997 | 9년 전 | 304 | ||
| 7996 | 9년 전 | 529 | ||
| 7995 | 9년 전 | 445 | ||
| 7994 | 9년 전 | 298 | ||
| 7993 | 9년 전 | 366 | ||
| 7992 | 9년 전 | 499 | ||
| 7991 | 9년 전 | 241 | ||
| 7990 | 9년 전 | 261 | ||
| 7989 | 9년 전 | 294 | ||
| 7988 | 9년 전 | 719 | ||
| 7987 | 9년 전 | 413 | ||
| 7986 | 9년 전 | 404 | ||
| 7985 | 9년 전 | 501 | ||
| 7984 | 9년 전 | 422 | ||
| 7983 | 9년 전 | 644 | ||
| 7982 | 9년 전 | 505 | ||
| 7981 | 9년 전 | 453 | ||
| 7980 | 9년 전 | 481 | ||
| 7979 | 9년 전 | 452 | ||
| 7978 | 9년 전 | 433 | ||
| 7977 | 9년 전 | 374 | ||
| 7976 | 9년 전 | 844 | ||
| 7975 | 9년 전 | 353 | ||
| 7974 | 9년 전 | 384 | ||
| 7973 | 9년 전 | 584 | ||
| 7972 | 9년 전 | 370 | ||
| 7971 | 9년 전 | 449 | ||
| 7970 | 9년 전 | 285 | ||
| 7969 | 9년 전 | 534 | ||
| 7968 | 9년 전 | 372 | ||
| 7967 | 9년 전 | 360 | ||
| 7966 | 9년 전 | 361 | ||
| 7965 |
|
9년 전 | 1007 | |
| 7964 | 9년 전 | 391 | ||
| 7963 | 9년 전 | 380 | ||
| 7962 | 9년 전 | 362 | ||
| 7961 |
전갈자리남자
|
9년 전 | 499 | |
| 7960 | 9년 전 | 955 | ||
| 7959 | 9년 전 | 544 | ||
| 7958 | 9년 전 | 400 | ||
| 7957 | 9년 전 | 360 | ||
| 7956 | 9년 전 | 365 | ||
| 7955 | 9년 전 | 435 | ||
| 7954 | 9년 전 | 364 | ||
| 7953 | 9년 전 | 419 | ||
| 7952 | 9년 전 | 345 | ||
| 7951 | 9년 전 | 492 | ||
| 7950 | 9년 전 | 380 | ||
| 7949 | 9년 전 | 381 | ||
| 7948 | 9년 전 | 318 | ||
| 7947 | 9년 전 | 923 | ||
| 7946 | 9년 전 | 415 | ||
| 7945 | 9년 전 | 384 | ||
| 7944 | 9년 전 | 397 | ||
| 7943 | 9년 전 | 360 | ||
| 7942 | 9년 전 | 400 | ||
| 7941 | 9년 전 | 379 | ||
| 7940 | 9년 전 | 871 | ||
| 7939 | 9년 전 | 335 | ||
| 7938 | 9년 전 | 367 | ||
| 7937 | 9년 전 | 269 | ||
| 7936 | 9년 전 | 872 | ||
| 7935 | 9년 전 | 431 | ||
| 7934 | 9년 전 | 397 | ||
| 7933 | 9년 전 | 487 | ||
| 7932 | 9년 전 | 456 | ||
| 7931 | 9년 전 | 513 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기