简体   繁体   中英

How to read a CSV file into a 2d Array PHP

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.

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM