This post shows how to generate CSV output from PHP code.

In PHP, the fputcsv() function generates a csv file when it is provided a valid two dimensional array.

Following is an example with explanation

// create a two dimensional array of data
$myarrays = array( array ('Canada', 'China'), array('9.985 million sq km', '9.597 million sq km'));

// open a file to write data
$filehandle = fopen('myfile.csv', 'w');

// loop through the array
foreach ($myarrays as $arr) {

    // write data to csv file 

// close the filehandle

The content of myfile.csv is:

'Canada','9.985 million sq km'
'China','9.597 million sq km'