这是while循环的示例。我需要找到最后一次迭代,以便不会在末尾添加逗号。这是怎么做到的?
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
$isfirst = true;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if ($isfirst){
$isfirst = false;
continue;
}
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, '['.(int)$timestamp.','.(float)$data[2].'],');
}
fclose($handle);
}
}
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
$isfirst = true;
$firstline = true;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if ($isfirst){
$isfirst = false;
continue;
}
if ($firstline) {
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, '['.(int)$timestamp.','.(float)$data[2].']');
$firstline = false;
} else {
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, ',['.(int)$timestamp.','.(float)$data[2].']');
}
}
fclose($handle);
}
}
答案 0 :(得分:1)
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
$firstLine = TRUE;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if (!$firstLine) {
fputs($out, ',');
} else {
firstLine = FALSE;
}
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, '['.(int)$timestamp.','.(float)$data[2].']');
}
fclose($handle);
}
}