此代码创建一个数组并添加属性“payment_sum”,即使$value['CardCode'] != $prevCode
为 false。您可以看到我正在打印用于测试的值,最后一行 509.85 正确求和,但根据此if
语句应该省略之前的数组。
if (($handle = fopen('upload/BEN-new.csv', "r")) === FALSE) {
die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$cardCodes = array();
$payments = array();
$details = array ();
while ($row = fgetcsv($handle, 1024, ",")) {
$cardCodes[] = array_combine($headers, $row);
}
$prevCode = '';
foreach ($cardCodes as $key => $value) {
if ($value['CardCode'] != $prevCode) {
$payments['payment_sum'] = $value['InvPayAmnt'];
}
else {
$payments['payment_sum'] += $value['InvPayAmnt'];
}
$prevCode = $value['CardCode'];
print_r ($payments);
}
fclose($handle);
印刷...
Array
(
[payment_sum] => 1055.40
)
Array
(
[payment_sum] => 550.00
)
Array
(
[payment_sum] => 100.00
)
Array
(
[payment_sum] => 287.33
)
Array
(
[payment_sum] => 509.85
)
首选输出
Array
(
[payment_sum] => 1055.40
)
Array
(
[payment_sum] => 550.00
)
Array
(
[payment_sum] => 100.00
)
Array
(
[payment_sum] => 509.85
Array (
[currTotal] => 287.33
[currTotal] => 222.52
)
)
CSV
BENV1072 1055.4
BENV1073 550
BENV5271 100
BENV5635 287.33
BENV5635 222.52