개발 꿀팁/PHP
Remove empty elements from PHP array
Jammie
2018. 1. 16. 01:24
반응형
Example 1)
Remove empty string elements
Elements made up of white space are not removed.
$arr = array("lemon", "", "", "\t\n", "orange");
$reduced_arr = array_filter($arr);
print_r($reduced_arr);
# Array
# (
# [0] => lemon
# [3] =>
#
# [4] => orange
# )
Example 2)
trim() removes whitespace from an empty string
$arr = array("lemon", "", "", "\t\n", "orange");
$reduced_arr = array_filter(array_map('trim',$arr));
print_r($reduced_arr);
# Array
# (
# [0] => lemon
# [4] => orange
# )
Example 3)
0 to organize into a base array.
$arr = array("lemon", "", "", "\t\n", "orange");
$reduced_arr = array_values(array_filter(array_map('trim',$arr)));
print_r($reduced_arr);
# Array
# (
# [0] => lemon
# [1] => orange
# )
반응형