PHP reading csv column into array

后端 未结 4 392
暖寄归人
暖寄归人 2021-01-12 18:52

I there a php function that enables me to read a csv column (COLUMN NOT LINE) into an array or a string ?

thank you in advance.

相关标签:
4条回答
  • 2021-01-12 19:20

    May this will help. http://www.php.net/manual/de/function.fgetcsv.php Just grab the position of the row for the column you want to have.

    0 讨论(0)
  • 2021-01-12 19:29
    $arr=array();
    $row = -1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
    
            $row++;
            for ($c = 0; $c < $num; $c++) {
                $arr[$row][$c]= $data[$c];
            }
        }
        fclose($handle);
    }
    
    0 讨论(0)
  • 2021-01-12 19:33

    You should give fgetcsv method a try. It lets you read a file line by line and returns associative array. This function is specially for reading from CSV files.

    In any case you will have to read each line even if you will have to process just a column.

    http://php.net/manual/en/function.fgetcsv.php

    0 讨论(0)
  • 2021-01-12 19:36
    $csv = array_map("str_getcsv", file("data.csv", "r")); 
    $header = array_shift($csv); 
    // Seperate the header from data
    
    $col = array_search("Value", $header); 
     foreach ($csv as $row) {      
     $array[] = $row[$col]; 
    }
    
    0 讨论(0)
提交回复
热议问题