A. Volg
A. Volg

Reputation: 307

Remove empty string in array of strings

It seems that there are dozens of topic with similar problem, and mostly all have the same answer: use array_filter, array_map. The problem is that I used them, but it didn't quite help. So I have an array (is built from data in csv file):

Array
(
[0] => Array
    (
        [0] => name
        [1] => title
        [2] => email
    )

[1] => Array
    (
        [0] => First
        [1] => title 1
        [2] => [email protected]
    )

[2] => Array
    (
        [0] => second
        [1] => title 1
        [2] => [email protected]
    )

[3] => Array
    (
        [0] => 
    )

[4] => Array
    (
        [0] => third
        [1] => title 1
        [2] => [email protected]
    )

[5] => Array
    (
        [0] => 
    )

[6] => Array
    (
        [0] => 
        [1] =>  
        [2] => 
    ) 
 )

I have to delete all empty arrays. So I use such code:

    while (($row = fgetcsv($file, 8192, ';')) !== false) {
        if (array(null) !== $row) { // ignore blank lines
            $csv[] = $row;
        }
    }
    $array = array_filter(array_map('array_filter', $csv));

$array now is:

 Array
(
[0] => Array
    (
        [0] => name
        [1] => title
        [2] => email
    )

[1] => Array
    (
        [0] => First
        [1] => title 1
        [2] => [email protected]
    )

[2] => Array
    (
        [0] => second
        [1] => title 1
        [2] => [email protected]
    )

[3] => Array
    (
        [0] => third
        [1] => title 1
        [2] => [email protected]
    )

[4] => Array
    (
        [1] =>  
    )
) 

Why there is a 4th array with empty value? I need to get rid of it. gettype($array[4][1]) = string

UPDATE

The csv has empty rows, and even just a ";" delimiter without any string. I cannot influence on the process of inserting data into this csv. The csv looks like this:

 1 row: name;title;email
 2 row: First;title 1;[email protected]
 3 row: second;title 1;[email protected]
 4 row:
 5 row: third;title 1;[email protected]
 6 row:
 7 row: ; ;

Upvotes: 1

Views: 686

Answers (1)

Sindhara
Sindhara

Reputation: 1473

and mostly all have the same answer: use array_filter, array_map.

array_filter is a good approach, but I wouldn't use array_map, but array_reduce:

$array = array_filter(
    $csv,
    function ($value) {
        return 0 < array_reduce(
            $value,
            function ($carry, $item) {
                return empty(trim($item)) ? $carry : $carry + 1;
            },
            0
        );
    }
);

With array_reduce, I count the non-empty elements in an array. And if there are zero non-empty elements, the array is thrown away with array_filter.

For reference, the PHP 7.4-syntax, which looks nicer in my eyes, but could be a bit confusing at first

$array = array_filter(
    $csv,
    fn ($val) => 0 < array_reduce(
        $val, 
        fn ($carry, $item) => empty(trim($item)) ? $carry : $carry + 1, 
        0
    )
);

Upvotes: 1

Related Questions