1

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: ; ;
2
  • 1
    It looks like the element in [6][1] as some whitespace in it. Commented Dec 15, 2020 at 16:02
  • you are right guys. There are empty rows. I will Update post with csv data, so you can see how it works. Commented Dec 15, 2020 at 16:18

1 Answer 1

1

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
    )
);
Sign up to request clarification or add additional context in comments.

3 Comments

Well, that is new for me. That worked perfectly! Thank you for not only the solution, but also for the explanation. Very helpful!
Just a quick question: what stands second 0 for by PHP 7.4-syntax?
Oh, sorry. Forgot it in the first one. It's the default parameter for the $carry. Have a look here: php.net/manual/en/function.array-reduce.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.