【发布时间】:2014-08-24 19:54:22
【问题描述】:
我有一个 CSV,每晚都会从批发商那里下载更新价格。
我需要做的是编辑价格列(第 2 列)并将当前值乘以 1.3 (30%)。
我用于读取提供的 CSV 并仅获取我需要的列的代码如下,但我似乎无法弄清楚如何编辑价格列。
<?php
// open the csv file in write mode
$fp = fopen('var/import/tb_prices.csv', 'w');
// read csv file
if (($handle = fopen("var/import/Cbl_4036_2408.csv", "r")) !== FALSE) {
$targetColumns = array(1, 2, 3); // get data from the 1st, 4th and 15th column
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$targetData = array(); // array that hold target data
foreach($targetColumns as $column){ // loop throught the targeted columns array
if($column[2]){
$data[$column] = $data[0] * 1.3;
}
$targetData[] = $data[$column]; // get the data from the column
}
# Populate the multidimensional array.
$csvarray[$nn] = $targetData; // add target data to csvarray
// write csv file
fputcsv($fp, $targetData);
}
fclose($handle);
fclose($fp);
echo "CSV File Written Successfully!";
}
?>
请有人指出我正确的方向,解释一下你是如何计算出这个函数的,这样我就可以同时学习了。
【问题讨论】:
-
您能从您的 CSV 文件中添加一些示例数据吗?