Jump to content

Need help with CSV file to Array PLZ

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
Please peoples im really desperate ive been trying for a very very long time now to get something done but i have no clue what goes wrong at all.

ATM im trying to learn working with CSV files and importing goes fine. Heres the code for it:

<?php
$list = array
    (
    "josef,durba,Bos,EN",
    "sjaqeline,toulors,paris,FR",
    );

    $file = fopen("c://temp.csv","w");

    foreach ($list as $line)
    {
    fputcsv($file,split(',',$line));
    }
    fclose($file);
?>
Now i want to read this file with the following code:
$bestand = fopen("c://temp.csv", "r");
        
        while (!feof($bestand)) {
            $csvArray = fgetcsv($bestand, 1024);

            foreach ($csvArray as $key => $value) {
               
                 print "key is: " . $key . " value is: " . $value . "<br>";
               
            }
           
        }
        fclose($bestand);
        ?>
however here lies my problem. When i execute the script it prints the following:
key is: 0 value is: josef
key is: 1 value is: durba
key is: 2 value is: Bos
key is: 3 value is: EN
key is: 0 value is: sjaqeline
key is: 1 value is: toulors
key is: 2 value is: paris
key is: 3 value is: FR

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\CSVproject\index.php on line 71

And im really clueless here i have NO idea how to get rid of the last error. It seems like everything is going fine but the error keeps popping up. So anyone have any ideas and can help me? Its really killing me!

Anyhow thanks in advance.

Edited by Jaan, 14 July 2010 - 09:19 PM.
Please use code tags when you are posting your codes!


#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
It tried to get a third line which does not exist. I believe files are saved with an extra line at the end of it so if you write from to it (append) it will start on a new line. But there is no value there. I just added a test to see if the variable was false or not and it didn't try to loop the third time. There is a simpler way to do it here: PHP: fgetcsv - Manual

<?PHP
echo "<pre>";
$fileName = "test.csv";
$list = array("josef,durba,Bos,EN","sjaqeline,toulors,paris,FR");
$file = fopen($fileName,"w");
foreach ($list as $line) {
    fputcsv($file,split(',',$line));
}
fclose($file);

$bestand = fopen($fileName, "r");
while (!feof($bestand)) {
    if (($csvArray = fgetcsv($bestand, 1024, ",")) !== FALSE) {
        print_r($csvArray);
        foreach ($csvArray as $key => $value) {
            print "key is: " . $key . " value is: " . $value . "<br>";
        }
    }
}
fclose($bestand);
echo "</pre>";


#3
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
Bit late reaction but thanks for the reply. Did it with fgetcsv now and it goes alot better :P