<?php
/**
 * Recevies an array and save it into a csv file
 *
 * @return string $logDir
 */
function array_To_CSV(array $data, string $csv = 'appLog.csv',$delimiter = ',', $enclosure = '"', $escape_char = "\\")
{
    $logDir = '../../WappsiAPI7.2/writable/logs/' . $csv;
    // File pointer in writable modee
    // echo $logDir;
    $file_pointer = fopen($logDir, 'w');
    // Traverse through the associative
    // array using for each loop
    $head = false;
    foreach ($data as $i) {

        // Write the data to the CSV file

        // $i['error'] = json_encode($i['error']);
        if(!$head){

            fputcsv($file_pointer, array_keys($i),$delimiter, $enclosure, $escape_char);
            $head = true;
        }else{
            fputcsv($file_pointer, $i,$delimiter, $enclosure, $escape_char);
        }

        

        // echo 'here';
    }

    // Close the file pointer.
    fclose($file_pointer);

    return $logDir;
}
