0

I'm trying to read a csv file into an array. Here's what I've got so far for reading the csv into an array:

$i = 0;
while ($i < $fileLines) {
    $userDB=Array(
        ($i) => Array
            (
                (title) => $harry[$i][0],
                (subtitle) => $harry[$i][1],
                (series) => $harry[$i][2],
                (author) => $harry[$i][3],
                (author2) => $harry[$i][4],
                (author3) => $harry[$i][5],
                (category) => $harry[$i][6],
                (subjects) => $harry[$i][7],
                (callnumber) => $harry[$i][8],
                (isbn) => $harry[$i][9],
                (publisher) => $harry[$i][10],
                (keywords) => $harry[$i][11],
                (option1) => $harry[$i][12],
                (option2) => $harry[$i][13],
                (option3) => $harry[$i][14],
                (option4) => $harry[$i][15],
                (option5) => $harry[$i][16],
                (option6) => $harry[$i][17]
            ) // end child array
    ); // end parent array
    $i++;
} // end while

Here is an example of the CVS I'm using:

"Algebra and Trigonometry with Analytic Geometry, Classic Edition",,,"Swokowski, Earl","Cole, Jeffery A.",,Cengage Learning,,,495559717,,,,," ALGEBRA AND TRIGONOMETRY WITH ANALYTIC GEOMETRY, CLASSIC EDITION 12 SWOKOWSKI, EARL COLE, JEFFERY A. CENGAGE LEARNING 2009-01-28 912 500 0495559717 "
All My Friends Are Dead,,,"Monsen, Avery","John, Jory",,Chronicle Books,,,811874559,,,,," ALL MY FRIENDS ARE DEAD MONSEN, AVERY JOHN, JORY CHRONICLE BOOKS 2010-06-30 96 500 0811874559 "
All My Friends Are Still Dead,,,"John, Jory","Monsen, Avery",,Chronicle Books,,,1452106967,,,,," ALL MY FRIENDS ARE STILL DEAD 3.2.2012 JOHN, JORY MONSEN, AVERY CHRONICLE BOOKS 2012-03-07 108 500 1452106967 "

My problem comes when I try to loop through writing to the array. If I comment out the while loop then I correctly read the first item in the CSV. However when I try and use the while loop then I don't get any data written to the array. What am I missing/doing completely wrong?

I haven't included all my code because I don't want to drown everyone, but I can include more if it will help.

2
  • If you're reading a CSV file, you should use freadcsv function or something similar. Commented Oct 27, 2014 at 15:15
  • 1
    Whats with the weird invalid syntax? Commented Oct 27, 2014 at 15:20

2 Answers 2

1

You have to declare your $userDB before the while loop :

$i = 0;
$userDB = array();
while ($i < $fileLines) {
    $userDB[] => Array
            (
                (title) => $harry[$i][0],
                (subtitle) => $harry[$i][1],
                (series) => $harry[$i][2],
                (author) => $harry[$i][3],
                (author2) => $harry[$i][4],
                (author3) => $harry[$i][5],
                (category) => $harry[$i][6],
                (subjects) => $harry[$i][7],
                (callnumber) => $harry[$i][8],
                (isbn) => $harry[$i][9],
                (publisher) => $harry[$i][10],
                (keywords) => $harry[$i][11],
                (option1) => $harry[$i][12],
                (option2) => $harry[$i][13],
                (option3) => $harry[$i][14],
                (option4) => $harry[$i][15],
                (option5) => $harry[$i][16],
                (option6) => $harry[$i][17]
            ) // end child array
    ); // end parent array
    $i++;
} // end while

BTW, why don't you directly use the fgetcsv or str_getcsv php function?

$userDB = str_getcsv($fileLines);
Sign up to request clarification or add additional context in comments.

4 Comments

one thing i would like to know regarding fgetcsv is that it takes 3 parameters like fgetcsv($handle, 1000, ",") where $handle=fopen("csvfile","r") now the 2nd param 1000 is related with any memory limit or line number/length?
See my edit and the code to replace yours with only one line.
Hi Veve, I tried your suggestion and declared $userDB before the while loop, however this didn't seem to solve my issue. When I try to echo array_values($userDB); all I get is array.
You can't "echo" an array, you have to "print_r" or "var_dump" it.
0

I used this method:

        $formattedArr = array(); // create array
        $filename = "sample.csv"; // point to CSV file

        if (($handle = fopen($filename, "r")) !== FALSE) {
            $key = 0; // set array key.
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $count = count($data);  //get total keys in row
                for ($i=0; $i < $count; $i++) { //insert data to our array
                    $formattedArr[$key][$i] = $data[$i];
                } // end for
                $key++;
            } // end while
            fclose($handle); //close file handle
        } // end if

It runs through the CSV and adds each item to a location (eg $formattedArr[0][0] = "Algebra and Trigonometry with Analytic Geometry, Classic Edition". Thank you all for your comments, suggestions, and answers.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.