php修改csv文件的方法:首先獲取當(dāng)前絕對(duì)路徑;然后每次讀取csv里面的一行內(nèi)容,代碼為【while ($data = fgetcsv($file))】;最后此為一個(gè)數(shù)組,要獲得每一個(gè)數(shù)據(jù),訪問(wèn)數(shù)組下標(biāo)即可。
php修改csv文件的方法:
php讀取csv文件并處理成以表頭為鍵名的數(shù)組
header("content-type: text/html; charset=gbk");//設(shè)置輸出編碼 ini_set('memory_limit', '1024m');//設(shè)置內(nèi)存 set_time_limit(0); //設(shè)置超時(shí) $dir = dirname(__file__).'/'; //獲取當(dāng)前絕對(duì)路徑 $row = 1;//第一行開(kāi)始 $file = fopen($dir.'5111.csv', "r"); # 結(jié)果 $res = array(); # 計(jì)數(shù)標(biāo)示 $header = []; $flag = false; $i = 0; while ($data = fgetcsv($file)) {//每次讀取csv里面的一行內(nèi)容 if (!$flag) { $header = $data;//此為一個(gè)數(shù)組,要獲得每一個(gè)數(shù)據(jù),訪問(wèn)數(shù)組下標(biāo)即可 $flag = true; } else { $temp = array_slice($data, 0,11);//取多少列數(shù)據(jù) foreach ($temp as $key => $value) { $index = $header[$key]; $res[$i][$index] = $value; } $i ; } }echo "<pre>";print_r($res);die;相關(guān)學(xué)習(xí)推薦:php編程(視頻)