Santosh Bohara Santosh Bohara - 2 months ago 7
PHP Question

Why i am getting blank array at end?

Why i am getting blank array at the end?

My PHP code :

$name = $_FILES["csv"]["tmp_name"];
$file = fopen($name,"r");

while(!feof($file)){

$row = fgetcsv($file);

$data = array(
"1" =>$row[0],
"2" =>$row[1],
"3" =>$row[2],
"4" =>$row[3]
);

echo "<pre>";
print_r($data);
}
fclose($file);


And my result :


Array
(
[1] => a
[2] => b
[3] => c
[4] => d
)

Array
(
[1] => e
[2] => f
[3] => g
[4] => h
)

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


Answer

Try Below code.If still it returns empty array at the end,that means your csv file may have blank space in one of the cell of last row.

 $name = $_FILES["csv"]["tmp_name"];

 $file = fopen($name,"r");

 while (($data = fgetcsv($file , 1000, ",")) !== FALSE) {

      echo "<pre>";

      print_r($data);

      echo "</pre>";

 }

 fclose($file);
Comments