如何将CSV文件读入2d数组PHP

时间:2014-10-27 15:12:26

标签: php arrays csv associative-array

我试图将csv文件读入数组。这是我到目前为止将csv读入数组所得到的:

$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

以下是我使用的CVS示例:

"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 "

当我尝试循环写入数组时,我的问题出现了。如果我注释掉while循环,那么我正确读取了CSV中的第一项。但是,当我尝试使用while循环时,我不会将任何数据写入数组。我错过了什么/完全错了?

我还没有包含我的所有代码,因为我不想淹死所有人,但如果有帮助我可以加入更多代码。

2 个答案:

答案 0 :(得分:1)

您必须在while循环之前声明$userDB

$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
顺便说一下,为什么不直接使用fgetcsvstr_getcsv php函数?

$userDB = str_getcsv($fileLines);

答案 1 :(得分:0)

我使用了这种方法:

        $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

它运行CSV并将每个项目添加到一个位置(例如$ formattedArr [0] [0] =“代数和三角学与解析几何,经典版”。感谢大家的意见,建议和答案。