Question

Ok, here's the thing.

if (!$fp=fopen($file,"r")) echo "The file could not be opened.<br/>";  
            while (( $data = fgetcsv ( $fp , 1000 , ',')) !== FALSE ) {
                // here, var_dump($data); shows the correct array
                $i = 0; 
                foreach ($data as $i=>$row ) {  
                   $matrix=explode( ',', $row);
                   // $matrix recieves only the first field of the csv line
                   // seems like I'm getting a field on each foreach iteration
                   // shouldn't I get the whole line each time?
                } //end foreach
            } //end while

I don't really see the problem here. By the way, this code works on my local machine, and doesn't work on my server. Both are linux, and php versions are the same.

Any thoughts? Thank you.

Was it helpful?

Solution

fgetscv() returns each row as an array. So your problem is this:

$data is a row, which is already an array. Each iteration in the while() loop will return a row. There is no need for an explode.

$allRowsAsArray = array();
if (!$fp=fopen($file,"r")) echo "The file could not be opened.<br/>";  
while (( $data = fgetcsv ( $fp , 1000 , ',')) !== FALSE ) 
{
    $allRowsAsArray[] = $data;
}
print_r($allRowsAsArray); //Will return your entire csv as an array of rows, each row as an array.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top