doushi1996 2014-10-27 15:12
浏览 28
已采纳

如何将CSV文件读入2d数组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.

  • 写回答

2条回答 默认 最新

  • dousao8152 2014-11-05 15:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器