Simplest way to read a csv file using php, and then select one specific value

前端 未结 2 1595
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 05:42

Thank you in advance for your time/help. I\'m a newbie learning php, so please keep that in mind.

1st Question I need a php script that reads a csv file.

2n

2条回答
  •  盖世英雄少女心
    2021-01-07 06:38

    If you need to select several cases, the best way is to make a multi-dimensional array in php. Otherwise, just throw a few counters into your while loops and done.

    $row = 1;
    $mycsvfile = array(); //define the main array.
    if (($handle = fopen("1.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "

    $num fields in line $row:

    \n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "
    \n"; } $mycsvfile[] = $data; //add the row to the main array. } fclose($handle); } echo $mycsvfile[3][1]; //prints the 4th row, second column.

提交回复
热议问题