我也试图用preg_replace删除空格,爆炸,修剪
`Array
(
[0] => s
[1] =>
[2] => s
[3] =>
[4] => a
[5] =>
[6] => i
[7] =>
[8] => 2
[9] =>
[10] => 2
)`
答案 0 :(得分:1)
您可以使用array_filter
删除空数组元素
$arr = array('s','','s','','a','i','',2,'',2);
$arr = array_filter($arr);
echo "<pre>";
print_r( $arr );
echo "</pre>";
这将导致:
Array
(
[0] => s
[2] => s
[4] => a
[5] => i
[7] => 2
[9] => 2
)
如果有多个空格,您可以使用trim
作为回调。像:
$arr = array('s',' ','s',' ','a','i',' ',2,'',2);
$arr = array_filter($arr,'trim');
echo "<pre>";
print_r( $arr );
echo "</pre>";
会得到相同的结果。
Doc:array_filter