-2
["Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:33, GENDER:MALE, Age:58,Bp_Systolic:130,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:48, GENDER:FEMALE, Age:62,Bp_Systolic:132,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:50, GENDER:MALE, Age:60,Bp_Systolic:120,Bp_Diastolic:70", "Boxname:MH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:51, GENDER:MALE, Age:63,Bp_Systolic:118,Bp_Diastolic:62"]

I want to convert this array into csv export file. I have tried code like this.

<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

$list=$name;
foreach ($list as $line)
  {
fputcsv($output, $line);
  }

?>

But i can t able to get the data. Can any body help me out. I have tried many ways. I think my array format giving trouble. Can anybody suggest array format from this to make the export easy.

divakar
  • 1,379
  • 6
  • 15
  • 31
  • Is $name actually an array, or is it a string? – Mark Baker Sep 09 '14 at 08:23
  • please check this link it might be helpful http://stackoverflow.com/questions/13108157/php-array-to-csv – Ritesh d joshi Sep 09 '14 at 08:23
  • @markbaker its an array. Actually $name[0]="Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80"; – divakar Sep 09 '14 at 08:25
  • The data you have shown is an array with only **one** element containing a long text value. – CBroe Sep 09 '14 at 08:32

2 Answers2

1

You are passing a string of concatenated values as rows to fputcsv() where it expects an array element for each column. Your data should be split into an array of column values for each row, I.E:

$list = Array(
    Array("Boxname:HH", "X1:53.3", "X2:106.6", "Y1:33.300000000000004", "Y2:59.947215189873425", "PID:22", "GENDER:MALE", "Age:63", "Bp_Systolic:120", "Bp_Diastolic:80"),
    Array("Boxname:HH", "X1:53.3", ..)
    //etc
)

Changing this array is how you are going to fix this. If you can't change the way the array is created, you can always remap it to have it in the format fputcsv() expects, using array_map():

$list = ["Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:33, GENDER:MALE, Age:58,Bp_Systolic:130,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:48, GENDER:FEMALE, Age:62,Bp_Systolic:132,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:50, GENDER:MALE, Age:60,Bp_Systolic:120,Bp_Diastolic:70", "Boxname:MH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:51, GENDER:MALE, Age:63,Bp_Systolic:118,Bp_Diastolic:62"];

$list = array_map(function($i){
    return array_map("trim", // Trim the values
        explode(",", $i) // Explode each row into an array of column values
    );
}, $list);

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

$output = fopen('php://output', 'w');

fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

foreach ($list as $line) {
    fputcsv($output, $line);
}

DEMO

George
  • 36,413
  • 9
  • 66
  • 103
  • Hey thanks for you explanation. Is this code will work? – divakar Sep 09 '14 at 08:45
  • If you dont mind can you clear me in one thing. I actually send the data from javascript array to php. using this code $.post( "export.php", { name: patient_array,header:header }) .done(function( data ) { alert( "Data Loaded: " + data ); document.location.href = 'export.php'; }); Am i need to serailize the array before sending to the PHP page. And I am receiving it using $_POST['name']. Is this right way to do? – divakar Sep 09 '14 at 09:11
  • If I were you, yes, I would normalize your array before posting it. But be sure to check the array before simply passing it to `fputcsv()` and producing errors. – George Sep 09 '14 at 10:31
  • Yea i think i Have problem with my array. I am trying to get it solved – divakar Sep 09 '14 at 10:49
0

This is an example of a working solution, maybe you could use this as a base:

$maxSize = count($commentArray);

    for ($c=0; $c < $maxSize; $c++) {
        $toCSV .= $nameArray[$c] . ";" . $amountArray[$c] . ";" . $dateArray[$c] . ";" . $commentArray[$c] . ";" . $bankaccount[$c] . "\n";
    }

file_put_contents($file, $toCSV);       

It loops through all elements of the arrays, separates the values by ;s and when the final element is reached then it puts a new line separator.

Once this is concatenated as a whole, file_put_contents creates a CSV file.

Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
  • That does not care of any “special characters” inside the data, which `fputcsv` already does perfectly well. – CBroe Sep 09 '14 at 08:34