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 = " ";
}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);
?>
$export_arrlook like$export_arr as $fieldsto$cars as $fields