유용한 함수 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년 전 | 371 | ||
| 8029 | 9년 전 | 310 | ||
| 8028 | 9년 전 | 264 | ||
| 8027 | 9년 전 | 281 | ||
| 8026 | 9년 전 | 337 | ||
| 8025 | 9년 전 | 385 | ||
| 8024 | 9년 전 | 344 | ||
| 8023 | 9년 전 | 394 | ||
| 8022 | 9년 전 | 322 | ||
| 8021 | 9년 전 | 328 | ||
| 8020 | 9년 전 | 320 | ||
| 8019 | 9년 전 | 344 | ||
| 8018 | 9년 전 | 447 | ||
| 8017 | 9년 전 | 539 | ||
| 8016 | 9년 전 | 329 | ||
| 8015 | 9년 전 | 391 | ||
| 8014 | 9년 전 | 326 | ||
| 8013 | 9년 전 | 236 | ||
| 8012 | 9년 전 | 244 | ||
| 8011 | 9년 전 | 446 | ||
| 8010 | 9년 전 | 301 | ||
| 8009 | 9년 전 | 291 | ||
| 8008 | 9년 전 | 288 | ||
| 8007 | 9년 전 | 431 | ||
| 8006 | 9년 전 | 462 | ||
| 8005 |
|
9년 전 | 969 | |
| 8004 | 9년 전 | 359 | ||
| 8003 | 9년 전 | 424 | ||
| 8002 | 9년 전 | 321 | ||
| 8001 |
|
9년 전 | 665 | |
| 8000 | 9년 전 | 421 | ||
| 7999 | 9년 전 | 375 | ||
| 7998 | 9년 전 | 430 | ||
| 7997 | 9년 전 | 311 | ||
| 7996 | 9년 전 | 534 | ||
| 7995 | 9년 전 | 457 | ||
| 7994 | 9년 전 | 336 | ||
| 7993 | 9년 전 | 397 | ||
| 7992 | 9년 전 | 509 | ||
| 7991 | 9년 전 | 252 | ||
| 7990 | 9년 전 | 288 | ||
| 7989 | 9년 전 | 305 | ||
| 7988 | 9년 전 | 730 | ||
| 7987 | 9년 전 | 429 | ||
| 7986 | 9년 전 | 421 | ||
| 7985 | 9년 전 | 511 | ||
| 7984 | 9년 전 | 431 | ||
| 7983 | 9년 전 | 662 | ||
| 7982 | 9년 전 | 523 | ||
| 7981 | 9년 전 | 473 | ||
| 7980 | 9년 전 | 491 | ||
| 7979 | 9년 전 | 476 | ||
| 7978 | 9년 전 | 457 | ||
| 7977 | 9년 전 | 392 | ||
| 7976 | 9년 전 | 850 | ||
| 7975 | 9년 전 | 361 | ||
| 7974 | 9년 전 | 400 | ||
| 7973 | 9년 전 | 598 | ||
| 7972 | 9년 전 | 385 | ||
| 7971 | 9년 전 | 458 | ||
| 7970 | 9년 전 | 303 | ||
| 7969 | 9년 전 | 544 | ||
| 7968 | 9년 전 | 385 | ||
| 7967 | 9년 전 | 373 | ||
| 7966 | 9년 전 | 372 | ||
| 7965 |
|
9년 전 | 1018 | |
| 7964 | 9년 전 | 398 | ||
| 7963 | 9년 전 | 399 | ||
| 7962 | 9년 전 | 379 | ||
| 7961 |
전갈자리남자
|
9년 전 | 505 | |
| 7960 | 9년 전 | 967 | ||
| 7959 | 9년 전 | 554 | ||
| 7958 | 9년 전 | 408 | ||
| 7957 | 9년 전 | 367 | ||
| 7956 | 9년 전 | 368 | ||
| 7955 | 9년 전 | 457 | ||
| 7954 | 9년 전 | 380 | ||
| 7953 | 9년 전 | 433 | ||
| 7952 | 9년 전 | 358 | ||
| 7951 | 9년 전 | 504 | ||
| 7950 | 9년 전 | 395 | ||
| 7949 | 9년 전 | 389 | ||
| 7948 | 9년 전 | 323 | ||
| 7947 | 9년 전 | 937 | ||
| 7946 | 9년 전 | 436 | ||
| 7945 | 9년 전 | 399 | ||
| 7944 | 9년 전 | 424 | ||
| 7943 | 9년 전 | 381 | ||
| 7942 | 9년 전 | 406 | ||
| 7941 | 9년 전 | 394 | ||
| 7940 | 9년 전 | 884 | ||
| 7939 | 9년 전 | 356 | ||
| 7938 | 9년 전 | 388 | ||
| 7937 | 9년 전 | 278 | ||
| 7936 | 9년 전 | 885 | ||
| 7935 | 9년 전 | 452 | ||
| 7934 | 9년 전 | 420 | ||
| 7933 | 9년 전 | 518 | ||
| 7932 | 9년 전 | 490 | ||
| 7931 | 9년 전 | 540 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기