1

I'm trying to insert the 2d array into a csv file

here is my code below

<?php
$cars = array( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100));
$fp = fopen('file.xls', 'w');
foreach ($cars as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);
?>

but the values are inserted in single row in csv file.

any ideas

5
  • what does $export_arr look like Commented Aug 13, 2013 at 5:52
  • $export_arr is the 2d array value Commented Aug 13, 2013 at 5:53
  • thanks for your reply. while i execute i got the errors (Parse error: parse error, expecting `')'' in C:\wamp\www\export\some.php on line 4) Commented Aug 13, 2013 at 5:58
  • 1
    @Max25 change $export_arr as $fields to $cars as $fields Commented Aug 13, 2013 at 6:00
  • @Max25 Have a look at my answer below. I'm not sure if that's the result you're looking for. Commented Aug 13, 2013 at 6:12

2 Answers 2

1

EDIT 1:

This version saves it to file without prompting to save and outputs in a table with a slight border. The border in the table is not written to file, it only outputs to screen.

<?php

$fp = fopen('file.xls', 'w');
$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));
$titleArray = array_keys($cars[0]);
$delimiter = "\t";
$filename="file.xls";
 
//Loop through each subarray, which are our data sets
foreach ($cars as $subArrayKey => $subArray) {
    //Separate each datapoint in the row with the delimiter
    $dataRowString = implode($delimiter, $subArray);

//  print $dataRowString . "\r\n"; // prints output to screen

fwrite($fp, $dataRowString . "\r\n");

} // keep this always end of routine

// start of cell formatting

$row = 1;
if (($handle = fopen("file.xls", "r")) !== FALSE) {
   
    echo '<table border="1" cellspacing="0" cellpadding="3">';
   
    while (($data = fgetcsv($handle, 1000, '\t')) !== FALSE) {
        $num = count($data);
        if ($row == 1) {
            echo '<thead><tr>';
        }else{
            echo '<tr>';
        }
       
        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />\n";
            if(empty($data[$c])) {
               $value = "&nbsp;";
            }else{
               $value = $data[$c];
            }
            if ($row == 1) {
                echo '<th>'.$value.'</th>';
            }else{
                echo '<td align="center">'.$value.'</td>';
            }
        }
       
        if ($row == 1) {
            echo '</tr></thead><tbody>';
        }else{
            echo '</tr>';
        }
        $row++;
    }
   
    echo '</tbody></table>';
    fclose($handle);
}

?>


EDIT 2:

This version will process the data then prompt to save the file.

Output:

Volvo   100 96
BMW 60  59
Toyota  110 100

PHP code:

<?php

$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));
 
$titleArray = array_keys($cars[0]);
 
$delimiter = "\t";
 
$filename="file.xls";
 
//Send headers
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
 
 
//Loop through each subarray, which are our data sets
foreach ($cars as $subArrayKey => $subArray) {
    //Separate each datapoint in the row with the delimiter
    $dataRowString = implode($delimiter, $subArray);
    print $dataRowString . "\r\n";
}
?>


1st version:

Output will be:

Volvo
100
96
BMW
60
59
Toyota
110
100

If this is the desired result, then this is the code to accomplish this:

<?php

$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));

$fp = fopen('file.xls', 'w');

foreach ($cars as $fields) {

    fputcsv($fp, $fields, "\n");

}
fclose($fp);
?>
Sign up to request clarification or add additional context in comments.

6 Comments

@Max25 Ok, let me see what I can do.
thanks for your reply. i changed the output format in your code. i need the output in separate cell in csv file
thanks fred. but i need the output a volvo 100 96 in a same row but each value to be inserted in each cell in csv file.
@Max25 You're welcome. I did find another way which may be what you're looking for, however it will process the data then prompt to save the file. Will include the code in my edit.
@Max25 You're welcome. Oh, and I came up with another version if you want to see it. It saves it to file without prompting to save and outputs in a table. The border in the table is not written to file, it only outputs to screen.
|
1

You need to seperate the contents with comma to write in seperate rows.

$export_arr =$_POST['dataString'];
$fp = fopen('file.xls', 'w');
foreach ($export_arr as $fields) {
    fputcsv($fp, $fields,",");
}
fclose($fp);

4 Comments

i got errors while i execute the code(Parse error: parse error, expecting `')'' in C:\wamp\www\export\some.php on line 4)
What ever the case delimiter has a default , so this is not the answer.
ya m changed the array name $export_arr into $cars but still i got a error(Warning: fputcsv() expects parameter 2 to be array, string given in C:\wamp\www\export\some.php on line 5)
thanks dude, but the problem i need to insert the values in each separate cell. inside array(cars[0] value) is insert in single cell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.