我有这样一个CSV:集团通过CSV一列,从另一个(PHP)的值相加
25/07/2016 3
25/07/2016 4
26/07/2016 4
26/07/2016 1
和输出,我期望的,它必须是这样
25/07/2016 7
26/07/2016 5
我想这样做只使用PHP。 我不得不提,我不使用MySQL,我不想。 有没有解决方案来做到这一点?
我有这样一个CSV:集团通过CSV一列,从另一个(PHP)的值相加
25/07/2016 3
25/07/2016 4
26/07/2016 4
26/07/2016 1
和输出,我期望的,它必须是这样
25/07/2016 7
26/07/2016 5
我想这样做只使用PHP。 我不得不提,我不使用MySQL,我不想。 有没有解决方案来做到这一点?
"25/07/2016","3"
"25/07/2016","4"
"26/07/2016","4"
"26/07/2016","1"
对于例如假设您的CSV文件包含上述数据。
$arr = array();
$handle = fopen("example.csv", "r");
while(!feof($handle))
{
$arrOfCSVLine = fgetcsv($handle);
$date = $arrOfCSVLine[0];
$no = $arrOfCSVLine[1];
if(!array_key_exists($data,$arr))
{
$arr[$date] = $no;
}
else
{
$arr[$date] += $no;
}
}
echo '<pre>';
print_r($arr);
检查此代码..
这就是我需要的!谢谢! – OPR
尝试类似这样的事情。这只是从this similar post编辑的版本。
$my_file = fopen('file.csv', 'rb');
$my_array = array();
while($row = fgetcsv($my_file)) {
$my_array[$row[0]] += $row[1];
}
$finalOutput = array();
$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
if(isset($finalOutput[$line[0]])){
$finalOutput[$line[0]] = $line[1] + $finalOutput[$line[0]];
} else {
$finalOutput[$line[0]] = $line[1];
}
}
print_r($finalOutput);
向我们展示你已经尝试了什么。 – user2100721
你想从文件中读取csv并创建输出文件csv? – Aminesrine