Reputation: 33
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] => )
Upvotes: 2
Views: 358
Reputation: 6857
You can simply use "array_filter" function to remove blank array, Please find link with full code required: https://eval.in/640133
You just need to map and filter the result array to remove the empty array.
Thanks
Upvotes: 1
Reputation: 631
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);
Upvotes: 0