Generate a CSV file from a multidimensional array in php
Encapsulate the csv line generation in another function.

function GenerateCSVlineFromArray($array){
    $line = "";
    foreach ($array as $value){
        $line .= $value .",";
    }
    //cut last comma
    $line = substr($line, 0, strlen($line) -1);
    return $line;
}


Assuming you have already the right order of values.