rbalarts
rbalarts

Reputation: 13

Remove whitespace or something from array php

Try to remove these whitespace array form this

Im also trying to remove whitespace with preg_replace, explode, trim

`Array
(
[0] => s
[1] => 
[2] => s
[3] => 
[4] => a
[5] => 
[6] => i
[7] => 
[8] => 2
[9] => 
[10] => 2
)`

Upvotes: 0

Views: 32

Answers (1)

Eddie
Eddie

Reputation: 26844

You can use array_filter to remove empty array elements

$arr = array('s','','s','','a','i','',2,'',2);

$arr = array_filter($arr);

echo "<pre>";
print_r( $arr );
echo "</pre>";

This will result to:

Array
(
    [0] => s
    [2] => s
    [4] => a
    [5] => i
    [7] => 2
    [9] => 2
)

Or you can use trim as callback if there are multiples spaces. Like:

$arr = array('s','   ','s','    ','a','i','   ',2,'',2);

$arr = array_filter($arr,'trim');

echo "<pre>";
print_r( $arr );
echo "</pre>";

Will get the same result.

Doc: array_filter

Upvotes: 1

Related Questions