0

I am storing one CSV file values into another through PHP. I want to save $col_1_values_array values in one column into CSV file and $col_2_values_array into another column. although my code is showing all values into a single row,how can i correct it?

here's my code

 $data_file = $ride[$i][0];
 $file_delimiter = ',';

 $csv = new csvCRUD($data_file,$file_delimiter);
 $col_1_values_array=$csv->output_column('C','array');
 $col_2_values_array=$csv->output_column('D','array');
 $c=count($col_1_values_array);
 $line=array();
 $fp = fopen('7654.csv', 'w');
 for($p=0;$p<$c;$p++)
 {
  $line[]=$col_1_values_array[$p];
  $line[]=$col_2_values_array[$p];
  }
  fputcsv($fp,$line);

  fclose($fp);
John Conde
  • 217,595
  • 99
  • 455
  • 496

1 Answers1

0

Move fputcsv() into your loop. That way a new row is created with each iteration. As of right now you're just making one big array which then is added as one big row.

for($p=0;$p<$c;$p++)
{
    $line[]=$col_1_values_array[$p];
    $line[]=$col_2_values_array[$p];
    fputcsv($fp,$line);
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • its showing into two colunms but because of loop previous values also come.hence new values come ahead of previous values.its result shape in csv file is :9.00 31.456 – user3140387 Mar 19 '14 at 21:24
  • its showing into two colunms but because of loop previous values also come.hence new values come ahead of previous values.its result shape in csv file is :9.00 31.456 . 9.00 31.456 9.02 31.467 . 9.00 31.456 9.02 31.467 9.04 31.568 how to correct it? – user3140387 Mar 19 '14 at 21:33