1

I got like this CSV:

1|bla.jpg|nature
2|       |earth

When I convert the CSV to array like this:

while (($line = fgetcsv($handle, 1000, ';')) !== FALSE)
{
    $data[] = $line;
}

then I get follow array in $data:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(7) "bla.jpg"
    [2]=>
    string(6) "nature"
  }
  [1]=>
  array(1) {
    [0]=>
    string(163) "2"
  }
}

But I need it like that:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(7) "bla.jpg"
    [2]=>
    string(6) "nature"
  }
  [1]=>
  array(3) {
    [0]=>
    string(1) "2"
    [1]=>
    string(0) ""
    [2]=>
    string(5) "earth"
  }
}

How can I keep parsing CSV when one field is empty?

Ignore it: I need to add some more text -.-

4 Answers 4

1

Set correct delimeter

while (($line = fgetcsv($handle, 1000, '|')) !== FALSE)

demo

Sign up to request clarification or add additional context in comments.

2 Comments

smh | isn't the delimeter I just use it here to make the table understandable/readable otherwise the first row wouldn't be parsed correctly
Why do you think is the first line parsed correctly?.................... the fields are separated with ;
0

Use file() to read it as an array, then explode each line with ";".

// For demo purpose
$str = "1;bla.jpg;nature
        2;;earth";
$arr = explode(PHP_EOL, $str);
// End
// $arr = file("file.csv");

Foreach($arr as &$line) $line = explode(";", $line);

Var_dump($arr);

https://3v4l.org/9FdVQ

Since it was unclear.

$arr = file("file.csv");

Foreach(array_splice($arr,0,10) as &$line) $line = explode(";", $line);

Var_dump($arr);

9 Comments

Ok but how do I get the CSV into $str? $str = fopen('bla.csv', "r"); isn't working this way
Read the comments. You should not read it to string. The string is just for demo purpose to make it run on 3v4l. Use the commented line $arr = file("file.csv");
And fopen does not return a string. If you want to read to string you need to use file_get_contents
Now it works thanks :D I never seen such weird syntax, could you also show how to get only first 10 lines? Ofc I could just slice the $arr after it's parsed completely but how could I only get first 10 lines from begine with?
See update with array_splice. What is the strange syntax
|
0

Maybe don't use fgetcsv, but read the entire line and use explode(';', $line) to split the line on each comma (or any other suitable delimiter ; csv files usually use ; but can also use , or tab)

Comments

0

For me this code works as expected,

you should try:

  • read another CSV file

  • check the encoding of CSV file is proper (I suppose it should be a UTF-8 encoding)

  • check, that delimiters in the file are the same like in the code

Look into php docs: http://php.net/manual/en/function.fgetcsv.php

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

Maybe this problem is related to operating system, which was used to create this CSV file?

Comments

Your Answer

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