导出 csv
<?php
// 头部标题
$csv_header = ['名称','性别','年龄'];
// 内容
$csv_body = [
['张三','男','13'],
['李四','女','13'],
['王五','男','13'],
['赵六','未知','13']
];
// 打开文件资源,不存在则创建
$fp = fopen('test.csv','a'); // 文件地址,默认根目录。可修改
// 处理头部标题
$header = implode(',', $csv_header) . PHP_EOL;
// 处理内容
$content = '';
foreach ($csv_body as $k => $v) {
$content .= implode(',', $v) . PHP_EOL;
}
// 拼接
$csv = $header.$content;
// 写入并关闭资源
fwrite($fp, $csv);
fclose($fp);
导入 csv
<?php
$handle = fopen(storage_path('app/is_tx_project.csv'), 'rb');
$data = [];
while (!feof($handle)) {
header('content-type: text/html; charset=utf-8');
$fields = fgetcsv($handle);
if($fields === false){
break;
}
$data[] = $fields;
}
fclose($handle);
print_r($data);
原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/268061.html