"PHP 배열에서 빈 문자열 지우기"의 두 판 사이의 차이

23번째 줄: 23번째 줄:
trim 후에 빈 원소 제거
trim 후에 빈 원소 제거
<source lang='php'>
<source lang='php'>
function xmp_print_r($arr) { echo '<xmp>'; print_r($arr); echo '</xmp>'; }
$arr = array("lemon", "", "", "\t\n", "orange");
$reduced_arr = array_filter(array_map('trim',$arr));
print_r($reduced_arr);


$arr = array("lemon", "\t\n", "orange", "", "");
# Array
$reduced_arr = array_filter(array_map('trim', $arr));
# (
xmp_print_r($reduced_arr);
#     [0] => lemon
</source>
#     [4] => orange
*예제: http://zetawiki.com/ex/php/remove_empty_elements2.php
# )
 
;실행결과
<source lang='text'>
Array
(
     [0] => lemon
     [2] => orange
)
</source>
</source>



2016년 3월 28일 (월) 15:52 판

배열에서 빈 원소를 제거
array_filter
remove empty elements

1 소스 코드 1

$arr = array("lemon", "", "", "\t\n", "orange");
$reduced_arr = array_filter($arr);
print_r($reduced_arr);

# Array
# (
#     [0] => lemon
#     [3] => 	
# 
#     [4] => orange
# )

2 소스 코드 2

trim 후에 빈 원소 제거

$arr = array("lemon", "", "", "\t\n", "orange");
$reduced_arr = array_filter(array_map('trim',$arr));
print_r($reduced_arr);

# Array
# (
#     [0] => lemon
#     [4] => orange
# )

3 같이 보기

4 주석


5 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}