Home >Backend Development >PHP Problem >How to modify csv file in php

How to modify csv file in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-28 13:19:214389browse

How to modify the csv file in php: first get the current absolute path; then read one line of content in the CSV each time, the code is [while ($data = fgetcsv($file))]; finally this is a Array, to get each data, just access the array subscript.

How to modify csv file in php

php method to modify the csv file:

php reads the csv file and processes it with the header as the key Array of names

    header("Content-type: text/html; charset=GBK");//设置输出编码
    ini_set('memory_limit', '1024M');//设置内存
    set_time_limit(0); //设置超时
    $dir = dirname(__FILE__).'/';  //获取当前绝对路径
    $row = 1;//第一行开始
    $file = fopen($dir.'5111.csv', "r");
    # 结果
    $res = array();
    # 计数标示
    $header = [];
    $flag = false;
    $i = 0;
    while ($data = fgetcsv($file)) {//每次读取CSV里面的一行内容
      if (!$flag) {
         $header = $data;//此为一个数组,要获得每一个数据,访问数组下标即可
         $flag = true;
      } else {
         $temp = array_slice($data, 0,11);//取多少列数据
         foreach ($temp as $key => $value) {
            $index = $header[$key];
            $res[$i][$index] = $value;
         }
         $i++;
      }
    }
echo "<pre class="brush:php;toolbar:false">";
print_r($res);
die;

Related learning recommendations:php programming(video)

The above is the detailed content of How to modify csv file in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn