我有一个像:
这样的数组`array (size=5)
0 => ""
1 => ""
2 => "foo"
3 => ""
4 => "bar"`
我想将所有空项移到最后,但要保留它们:
array (size=5)
0 => "foo"
1 => "bar"
2 => ""
3 => ""
4 => ""
我尝试array_values(array_filter($myTab))
,但结果为:
`array (size=2)
0 => "foo"
1 => "bar"
`
如何在php中保留空项?
答案 0 :(得分:2)
你可以在一行中完成。我确实做了一些推杆评论
$myTab = array (
"",
"",
"foo",
"",
"bar");
// all not empty values
$a = array_filter($myTab);
// all empty values (rest in array)
$b = array_diff($myTab, $a);
// Full array
$new = array_merge($a, $b);
var_dump($new);
结果
array(5) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
}
答案 1 :(得分:0)
如果订单不重要,您可以使用此功能:
rsort($myTabs);