1 回答
TA贡献1862条经验 获得超7个赞
考虑到您的 csv 文件的名称是a.csv,这里是代码
<?php
$newCsv = array();
// Get the file in an array
$csv = array_map('str_getcsv', file('a.csv'));
foreach($csv as $row){
//Get the first column
$str = $row[0];
// Split the string
$strParts = explode('#', $str);
$size = sizeof($strParts);
// If it contains #?
if($size > 1){
$newFirstRow = "'location' : '". $strParts[$size -1]."'";
} else{
$newFirstRow = $row[0];
}
array_push($newCsv, array($newFirstRow, $row[1], $row[2]));
}
// Open the file in write mode ('w')
// Remember, this will rewrite the file completely
// If you want to backup the file
// do it before executing this code
$fp = fopen('a.csv', 'w');
// Loop through file pointer and a line
foreach ($newCsv as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
- 1 回答
- 0 关注
- 83 浏览
添加回答
举报