Skip to content

Instantly share code, notes, and snippets.

@ibes
Created September 24, 2013 13:36
Show Gist options
  • Save ibes/6684800 to your computer and use it in GitHub Desktop.
Save ibes/6684800 to your computer and use it in GitHub Desktop.
Custom Array to CSV function.
<?php
/**
* create CSV file from address data
*
* @var array Address Data
* @return string formated CSV file
*/
function createCsv($address) {
// build array with headlines
$lines = array(
array('BestellNr', 'Bestelldatum', 'EndKdNr',
'LieferName', 'LieferAnsprech', 'LieferStrasse', 'LieferLand',
'LieferPlz', 'LieferOrt', 'Pos',
'ArtikelNr', 'Menge', 'Einzelpreis', 'Währung')
);
$i = 1;
foreach ($this->Bestelldaten as $Produktdaten) {
$Pos = $i;
$lines[] = array($this->BestellNr, $this->Bestelldatum, $this->EndKdNr,
trim($this->LieferName), trim($this->LieferAnsprech), trim($address['LieferStrasse']), $address['LieferLand'],
trim($address['LieferPlz']), trim($address['LieferOrt']), $Pos,
$Produktdaten['ArtikelNr'], $Produktdaten['Menge'], number_format( $Produktdaten['Einzelpreis'] , 2, ',', '' ), 'EUR');
$i++;
}
unset($i);
// put array to csv
$outstream = fopen("php://temp", 'W+');
foreach ($lines as $fields) {
fwrite($outstream,implode(';',$fields) . "\r\n");
}
rewind($outstream);
$csv = stream_get_contents($outstream);
fclose($outstream);
$this->logger->logWrite("CSV-Datei generiert");
return $csv;
}
@ibes
Copy link
Author

ibes commented Sep 24, 2013

Maybe start the $csv with "\xEF\xBB\xBF" solves the problem, that the generated file is not UTF-8 by default.
As suggested: http://stackoverflow.com/questions/4839402/how-to-write-file-in-utf-8-format

Have to wait for feedback of Integralis...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment