我有一个CSV文件,我正在使用PHP阅读。它是一个逗号分隔的CSV文件,它将每行数据拆分为一个数组。在这种情况下,数组是$fields
。完成所有处理后,数组将作为XML文件吐出。
$fields[ 0 ]
包含每行的参考编号。某些行可能具有相同的引用号,表示项目在一起,因此$total_gross_weight
将是当前引用的值+下一个引用。
我有一个for循环遍历数组并处理它们。
For Loop
//set variable to empty
$previous_consignee_id = "";
$total_gross_weight = 0;
$total_volume = 0;
$total_net_weight = 0;
//for loop to loop over arrays
for( $counter = 0; $counter < count( $buffer ); $counter++ ) {
//$fields is the array, split with a comma
$fields = split( ",", $buffer[ $counter ] );
//set consignee_id as the first field in each array
//example of consignee_id = DUN72049214
$consignee_id = trim( $fields[ 0 ] );
//if the consignee_id is not equal to $previous_consignee_id
//and $previous_consignee_id is not empty
if( $consignee_id != $previous_consignee_id ) {
//set $total_gross_weight to fields[ 60 ]
//$fields[ 60 ] is always the weight
$total_gross_weight = $fields[ 60 ];
//run create_job method
//this method works fine so no changes needed to it
create_job( $xml, $fields, $total_gross_weight );
echo "creating new job ".$consignee_id." weight is ".$total_gross_weight."\n";
} else {
//if the $consignee_id matches the previous_consignee_id
//total_gross_weight is the current $total_gross_weight + $fields[ 60 ]
$total_gross_weight = $total_gross_weight + $fields[ 60 ];
echo "same job ".$consignee_id." weight is ".$total_gross_weight."\n";
}
//set $previous_consignee_id to the current $consignee_id;
$previous_consignee_id = $consignee_id;
}//endfor
For循环结果
creating new job DN1234567 weight is 500
creating new job DN1234568 weight is 500
same job DN1234568 weight is 1000
creating new job DN1234569 weight is 500
creating new job DN1234570 weight is 500
creating new job DN1234571 weight is 500
creating new job DN1234572 weight is 500
same job DN1234572 weight is 1000
same job DN1234572 weight is 1500
creating new job DN1234573 weight is 500
creating new job DN1234574 weight is 500
creating new job DN1234575 weight is 500
same job DN1234575 weight is 1000
creating new job DN1234576 weight is 500
creating new job DN1234577 weight is 500
same job DN1234577 weight is 1000
creating new job DN1234578 weight is 500
creating new job DN1234579 weight is 500
creating new job DN1234580 weight is 500
问题在于,第一份工作(DN1234567)正在回应“同样的工作”。然而,由于它是第一个,它应该回应为“新工作”。如您所见,当作业参考与前一个相同时,权重将合计在一起以将其合并为一个。但是,之后的后续作业具有不正确的值,因为它似乎带有“相同作业”值。
我尝试在每次迭代后取消设置变量,但我似乎无法弄明白。我认为If语句的条件存在问题,因为在第一次迭代中“previous_consignee_id”为空,导致它跳到else语句。
任何人都可以就如何解决这个问题给我一些指示。
答案 0 :(得分:0)
问题出在if语句中,如果你的条件是$previous_consignee_id != ""
,这将成为假第一次迭代,因为你在开始时指定""
所以删除它
if( $consignee_id != $previous_consignee_id && $previous_consignee_id != "" ) {
要
if( $consignee_id != $previous_consignee_id) {
修改强>
如果您在分配之前也显示值$total_gross_weight
。因此,请将您的代码更改为
if( $consignee_id != $previous_consignee_id) {
//run create_job method
//this method works fine so no changes needed to it
create_job( $xml, $fields, $total_gross_weight );
//set $total_gross_weight to fields[ 60 ]
//$fields[ 60 ] is always the weight
$total_gross_weight = $fields[ 60 ];
echo "creating new job ".$consignee_id." weight is ".$total_gross_weight."\n";
}