Return to Snippet

Revision: 41435
at February 18, 2011 00:47 by jorisros


Initial Code
<?php

$arrPerson['name'] = 'Piet';
$arrPerson['lastname'] = 'Janse';
$arrPerson['address'] = 'Thuisstraat 35';
$arrPerson['zipcode'] = '1111AA';
$arrPerson['email'] = '[email protected]';

$arrPersons[] = $arrPerson;

$arrPerson['name'] = 'Jan';
$arrPerson['lastname'] = 'Slagerszoon';
$arrPerson['address'] = 'Straat 135';
$arrPerson['zipcode'] = '1111AA';
$arrPerson['email'] = '[email protected]';

$arrPersons[] = $arrPerson;


$strCSV = '';

//Create titles from first record
if(is_array($arrPersons[0]))
{
	foreach($arrPersons[0] as $strField => $strValue)
	{
		$arrColomns[] = $strField;
	}
}
$strCSV .= "'".implode("','", $arrColomns)."'
";


//fill rows
if(is_array($arrPersons))
{
	$strRows = '';

	foreach($arrPersons as $arrPerson)
	{

		if(is_array($arrColomns))
		{
			$i = 0;
			$arrRow = array();

			foreach($arrColomns as $strColomn)
			{
				$arrRow[] = $arrPerson[$strColomn];
			}
			$strCSV  .= "'".implode("','",$arrRow)."'
";

		}
	}
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"persons.csv\"");
echo $strCSV;

Initial URL

                                

Initial Description

                                

Initial Title
Save persons as csv example

Initial Tags
php, csv

Initial Language
PHP