亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

PHP讀取和寫入CSV文件的示例代碼

瀏覽:108日期:2022-06-10 17:07:39
目錄
  • 1. 什么是 CSV 文件
  • 2. 從 CSV 文件中讀取數據
  • 3. 將數據寫入 CSV 文件

1. 什么是 CSV 文件

CSV(逗號分隔值)文件是使用逗號分隔信息的文本文件。該文件的每一行都是一條數據記錄,也就意味著它可以用于以表格的形式展現信息。

2. 從 CSV 文件中讀取數據

我將使用內置函數 file 從 CSV 文件中讀取數據,然后使用 str_getcsv() 解析包含逗號的字符串。

在介紹如何使用str_getcsv() 函數之前,我想向你介紹如何輸出 CSV 文件中的數據。

<?php    if($_FILES){var_dump(file($_FILES["file"]["tmp_name"], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));    }?>?<html>    <body><form method="post" enctype="multipart/form-data">    <input type="file" name="file" />    <button>upload</button></form>    </body></html>

當我使用上面的代碼上傳文件時,輸出以下數據:

如圖所示,每個字符串中都有逗號,每個逗號將一條信息與另一條信息隔開。

使用 array_map() 函數,并且 str_getcsv() 作為回調函數,該回調將解析每個具有逗號的字符串并將它們分隔在一個數組中。

if($_FILES){    //loop through the csv file into an array    $theCSV = array_map("str_getcsv", file($_FILES["file"]["tmp_name"], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));    //dump result    var_dump($theCSV);}

輸出如下:

輸出的數據看起來比之前要好得多,我們將列標題(全名、QQ、電子郵件)作為該數組的第一個元素。

我們使用 array_walk() 函數遍歷此數組 ,然后提供一個回調函數,它將列標題(全名、QQ、電子郵件)和每個 CSV 數據組合為一個新數組。

if($_FILES){    //loop through the csv file into an array    $theCSV = array_map("str_getcsv", file($_FILES["file"]["tmp_name"], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));    /*Walk through the array and combine the headers which is the first element of our csv array with the rest of the csv data*/    array_walk($theCSV, function(&$ary) use($theCSV) {$ary = array_combine($theCSV[0], $ary);    });    //dump result    var_dump($theCSV);}?>

注意,在上面的回調函數中,我使用了變量& 運算符將 $ary 通過引用傳遞給函數,這使的我們可以修改原始數組。當我們運行上面的代碼時,這就是我們的 CSV 數組現在的樣子:

注意這里有個問題:這個新數組的第一個元素是表頭,因為我們之前讓它與 CSV 數組的其他數組組裝在了一起。可以使用 array_shift() 來解決這個問題。

if($_FILES){    //loop through the csv file into an array    $theCSV = array_map("str_getcsv", file($_FILES["file"]["tmp_name"], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));    /*Walk through the array and combine the headers which is the first element of our csv array with the rest of the csv data*/    array_walk($theCSV, function(&$ary) use($theCSV) {$ary = array_combine($theCSV[0], $ary);    });    //remove column headers which is the first element    array_shift($theCSV);    //dump result    var_dump($theCSV);}

這就是我們最終的 CSV 數組的樣子

將上面的代碼封裝成一個函數,如下:

function readCSV($file){    if(empty($file) || !file_exists($file)) return;    //store the column headers    $headers = null;    $theCSV = array_map("str_getcsv", file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));    /*Walk through the array and combine the headers which is the first element of our csv array with the rest of the csv data*/    array_walk($theCSV, function(&$ary) use($theCSV, &$headers) {    $ary = array_combine($theCSV[0], $ary);    //store the headers    $headers = $theCSV[0];    });    //remove column headers which is the first element of our csv array    array_shift($theCSV);    //return data    return array("headers" => $headers,"data" => $theCSV    );}

3. 將數據寫入 CSV 文件

將數據寫入 CSV 文件,其邏輯是使用 fopen() 函數以附加模式打開 CSV 文件, 然后用 fputcsv() 解析我們要寫入 CSV 文件的數據,然后此方法將這些數據寫入文件流當中。

if($_SERVER["REQUEST_METHOD"] == "POST"){    $file = "./my_csv_file.csv";    //loop through the csv file into an array    $csvData = readCSV($file);    //create array to store the new data    $newData = [];    //loop through headers and then add values as a new array    foreach($csvData["headers"] as $index => $key){if($key == "Full Name"){    $newData[$key] = $_POST["full_name"];}elseif($key == "Email"){    $newData[$key] = $_POST["email"];}elseif($key == "Phone"){    $newData[$key] = $_POST["phone"];}else{    $newData[$key] = "";}    }    var_dump($newData);}

如圖所示就是我們將寫入到 CSV 文件的數組的數據

在我們將這些數據寫入到 CSV 文件之前,我們必須去掉 key,我們可以使用 array_values() 函數

if($_SERVER["REQUEST_METHOD"] == "POST"){    $file = "./my_csv_file.csv";    //loop through the csv file into an array    $csvData = readCSV($file);    //create array to store the new data    $newData = [];    //loop through headers and then add values as a new array    foreach($csvData["headers"] as $index => $key){if($key == "Full Name"){    $newData[$key] = $_POST["full_name"];}elseif($key == "Email"){    $newData[$key] = $_POST["email"];}elseif($key == "Phone"){    $newData[$key] = $_POST["phone"];}else{    $newData[$key] = "";}    }    //open the csv file as in append mode    $fp = fopen($file, "a+");    //remove keys from new data    $newData = array_values($newData);    //append data to csv file    fputcsv($f, $newData);    //close the resource    fclose($fp);}

不出意外的話,數據就會成功寫入到 CSV 文件當中去了。

以上就是PHP讀取和寫入CSV文件的示例代碼的詳細內容,更多關于PHP CSV文件的資料請關注其它相關文章!

標簽: PHP
主站蜘蛛池模板: 乱乳性高清 | 国产不卡一区二区视频免费 | 精品老司机在线视频香蕉 | 国产精品国产三级国产专播下 | 黄片毛片一级片 | 欧美亚洲尤物久久精品 | 亚洲激情在线看 | 欧美日韩在线一本卡 | 成年偏黄网站站免费 | 国产精品拍拍拍福利在线观看 | 青青草青青操 | 日本一级毛片私人影院 | 亚洲一级二级三级 | 亚洲一区二区三区不卡在线播放 | 真实男女xx00动态视频免费 | 91短视频版在线观看免费大全 | 国产精品jvid在线观看 | 国产青草视频 | 国产精品国产三级国产普通 | 免费黄视频在线观看 | 69成人做爰视频在线观看 | 午夜一级毛片 | 日本免费va毛片在线看大 | 视频在线观看免费播放www | 久久一精品 | 一级做a爰片久久毛片美女 一级做a爰片久久毛片唾 | 五月天爱爱 | 国产精品爱久久久久久久 | 亚洲国产一成人久久精品 | 日韩中文字幕不卡 | 欧美国产激情二区三区 | 在线黄色大片 | 一级欧美一级日韩 | 九九免费精品视频在这里 | 国产精品观看在线亚洲人成网 | 亚洲色图日韩 | 欧美一二区视频 | 亚洲日韩中文字幕天堂不卡 | 国产精品短视频 | 欧美a级成人淫片免费看 | 欧美特级黄 |