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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP讀取和寫(xiě)入CSV文件的示例代碼

瀏覽:63日期:2022-06-10 17:07:39
目錄
  • 1. 什么是 CSV 文件
  • 2. 從 CSV 文件中讀取數(shù)據(jù)
  • 3. 將數(shù)據(jù)寫(xiě)入 CSV 文件

1. 什么是 CSV 文件

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

2. 從 CSV 文件中讀取數(shù)據(jù)

我將使用內(nèi)置函數(shù) file 從 CSV 文件中讀取數(shù)據(jù),然后使用 str_getcsv() 解析包含逗號(hào)的字符串。

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

<?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>

當(dāng)我使用上面的代碼上傳文件時(shí),輸出以下數(shù)據(jù):

如圖所示,每個(gè)字符串中都有逗號(hào),每個(gè)逗號(hào)將一條信息與另一條信息隔開(kāi)。

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

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);}

輸出如下:

輸出的數(shù)據(jù)看起來(lái)比之前要好得多,我們將列標(biāo)題(全名、QQ、電子郵件)作為該數(shù)組的第一個(gè)元素。

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

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);}?>

注意,在上面的回調(diào)函數(shù)中,我使用了變量& 運(yùn)算符將 $ary 通過(guò)引用傳遞給函數(shù),這使的我們可以修改原始數(shù)組。當(dāng)我們運(yùn)行上面的代碼時(shí),這就是我們的 CSV 數(shù)組現(xiàn)在的樣子:

注意這里有個(gè)問(wèn)題:這個(gè)新數(shù)組的第一個(gè)元素是表頭,因?yàn)槲覀冎白屗c CSV 數(shù)組的其他數(shù)組組裝在了一起。可以使用 array_shift() 來(lái)解決這個(gè)問(wèn)題。

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 數(shù)組的樣子

將上面的代碼封裝成一個(gè)函數(shù),如下:

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. 將數(shù)據(jù)寫(xiě)入 CSV 文件

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

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);}

如圖所示就是我們將寫(xiě)入到 CSV 文件的數(shù)組的數(shù)據(jù)

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

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);}

不出意外的話,數(shù)據(jù)就會(huì)成功寫(xiě)入到 CSV 文件當(dāng)中去了。

以上就是PHP讀取和寫(xiě)入CSV文件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于PHP CSV文件的資料請(qǐng)關(guān)注其它相關(guān)文章!

標(biāo)簽: PHP
主站蜘蛛池模板: 中文字幕黄色片 | 国产的老妇人 | vr欧美乱强伦xxxxx | 国内成人精品亚洲日本语音 | 色天天天综合色天天碰 | 香蕉视频在线观看网址 | 三级视频在线播放线观看 | 国产videos| 久久综合久久综合久久综合 | 国产亚洲欧美日本一二三本道 | 日本成人黄色片 | 一级网站在线观看 | 久久综合给合久久狠狠狠色97 | 国产在线播放拍拍拍 | www视频在线免费观看 | 久久www成人看片 | 男人粗大一出一进女人下面视频 | 在线观看222www | 九九热这里只有 | 欧美成人在线影院 | 美女黄色免费在线观看 | 中国一级黄色片子 | 国产亚洲欧美一区二区三区 | 污污美女网站 | 99久久综合狠狠综合久久一区 | c看欧美激情毛片 | 国产激情毛片 | 国产精品一区欧美激情 | 黄黄网站 | 艾小青亚洲专区在线播放 | 日韩 第一页 | 欧美午夜精品 | 国产欧美日韩在线观看一区二区三区 | 日韩精品久久久毛片一区二区 | 欧美在线网址 | 一道本一区二区三区 | 国内精品91最新在线观看 | 中文在线观看视频 | 久久99精品久久久久久国产越南 | 成人免费观看视频久爱网 | 国产男女性特黄录像 |