유용한 함수 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"));
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5730 | 13년 전 | 771 | ||
| 5729 | 13년 전 | 3587 | ||
| 5728 | 13년 전 | 1159 | ||
| 5727 |
freejazzme
|
13년 전 | 1112 | |
| 5726 | 13년 전 | 1448 | ||
| 5725 |
itlang
|
13년 전 | 432 | |
| 5724 | 13년 전 | 513 | ||
| 5723 |
crocojea
|
13년 전 | 1494 | |
| 5722 | 13년 전 | 1984 | ||
| 5721 | 13년 전 | 714 | ||
| 5720 |
쿠우우우우
|
13년 전 | 472 | |
| 5719 | 13년 전 | 615 | ||
| 5718 |
|
13년 전 | 592 | |
| 5717 | 13년 전 | 1451 | ||
| 5716 |
|
13년 전 | 1041 | |
| 5715 | 13년 전 | 468 | ||
| 5714 | 13년 전 | 472 | ||
| 5713 | 13년 전 | 537 | ||
| 5712 |
|
13년 전 | 775 | |
| 5711 | 13년 전 | 697 | ||
| 5710 |
minini
|
13년 전 | 490 | |
| 5709 |
|
13년 전 | 1332 | |
| 5708 | 13년 전 | 684 | ||
| 5707 | 13년 전 | 5859 | ||
| 5706 |
오니기리죠
|
13년 전 | 1026 | |
| 5705 |
HSEngine
|
13년 전 | 658 | |
| 5704 |
Giggle
|
13년 전 | 1699 | |
| 5703 | 13년 전 | 506 | ||
| 5702 |
AMDbest
|
13년 전 | 1422 | |
| 5701 |
|
13년 전 | 2891 | |
| 5700 | 13년 전 | 504 | ||
| 5699 |
미션임파썩을
|
13년 전 | 540 | |
| 5698 | 13년 전 | 625 | ||
| 5697 | 13년 전 | 539 | ||
| 5696 | 13년 전 | 446 | ||
| 5695 |
|
13년 전 | 703 | |
| 5694 | 13년 전 | 444 | ||
| 5693 | 13년 전 | 977 | ||
| 5692 | 13년 전 | 950 | ||
| 5691 |
쉽다zzz
|
13년 전 | 718 | |
| 5690 | 13년 전 | 474 | ||
| 5689 | 13년 전 | 634 | ||
| 5688 | 13년 전 | 1414 | ||
| 5687 | 13년 전 | 467 | ||
| 5686 | 13년 전 | 2452 | ||
| 5685 |
AMDbest
|
13년 전 | 2158 | |
| 5684 | 13년 전 | 2038 | ||
| 5683 | 13년 전 | 1357 | ||
| 5682 |
AMDbest
|
13년 전 | 688 | |
| 5681 | 13년 전 | 3805 | ||
| 5680 |
|
13년 전 | 1683 | |
| 5679 | 13년 전 | 2089 | ||
| 5678 |
|
13년 전 | 526 | |
| 5677 |
mobiler
|
13년 전 | 465 | |
| 5676 | 13년 전 | 1604 | ||
| 5675 | 13년 전 | 588 | ||
| 5674 |
PHPㅡASP프로그래머
|
13년 전 | 1820 | |
| 5673 |
PHPㅡASP프로그래머
|
13년 전 | 1491 | |
| 5672 |
PHPㅡASP프로그래머
|
13년 전 | 1280 | |
| 5671 |
PHPㅡASP프로그래머
|
13년 전 | 1212 | |
| 5670 |
PHPㅡASP프로그래머
|
13년 전 | 4610 | |
| 5669 | 13년 전 | 3050 | ||
| 5668 | 13년 전 | 1412 | ||
| 5667 | 13년 전 | 493 | ||
| 5666 | 13년 전 | 1142 | ||
| 5665 |
Darby
|
13년 전 | 1606 | |
| 5664 | 13년 전 | 1338 | ||
| 5663 | 13년 전 | 1123 | ||
| 5662 | 13년 전 | 788 | ||
| 5661 | 13년 전 | 450 | ||
| 5660 | 13년 전 | 841 | ||
| 5659 | 13년 전 | 710 | ||
| 5658 | 13년 전 | 531 | ||
| 5657 | 13년 전 | 536 | ||
| 5656 | 13년 전 | 991 | ||
| 5655 | 13년 전 | 437 | ||
| 5654 | 13년 전 | 390 | ||
| 5653 |
졸린다젠장
|
13년 전 | 477 | |
| 5652 |
|
13년 전 | 396 | |
| 5651 | 13년 전 | 1028 | ||
| 5650 | 13년 전 | 720 | ||
| 5649 | 13년 전 | 2232 | ||
| 5648 | 13년 전 | 479 | ||
| 5647 | 13년 전 | 757 | ||
| 5646 | 13년 전 | 1493 | ||
| 5645 | 13년 전 | 679 | ||
| 5644 | 13년 전 | 614 | ||
| 5643 |
|
13년 전 | 573 | |
| 5642 |
쿠마하우스
|
13년 전 | 685 | |
| 5641 |
goaway
|
13년 전 | 1084 | |
| 5640 |
쿠마하우스
|
13년 전 | 647 | |
| 5639 | 13년 전 | 674 | ||
| 5638 |
쿠마하우스
|
13년 전 | 989 | |
| 5637 | 13년 전 | 813 | ||
| 5636 | 13년 전 | 1415 | ||
| 5635 |
goaway
|
13년 전 | 910 | |
| 5634 |
lllolll
|
13년 전 | 418 | |
| 5633 | 13년 전 | 963 | ||
| 5632 | 13년 전 | 4272 | ||
| 5631 | 13년 전 | 568 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기