function csvToJson($filename, $separator = ",") { //create the resulting array $result = array("records" => array()); //check if the file handle is valid //echo $filename; if (($handle = fopen($filename, "r")) !== false) { //"Contact","Company","Business Email","Business Phone","Direct Phone","Time Zone","Fax","Web","Source" //check if the provided file has the right format if (($data = fgetcsv($handle, 4096, $separator)) == false || ($data[0] != "Contact" || $data[1] != "Company" || $data[2] != "Business Email")) { throw new InvalidImportFileFormatException(sprintf('The provided file (%s) has the wrong format!', $filename)); } $db = new MyDB(); if (!$db) { echo $db->lastErrorMsg(); } else { echo "Opened database successfully\n"; } //loop through your data while (($data = fgetcsv($handle, 4096, $separator)) !== false) { $TZ = $db->UpdateTimeZone($data[3]); echo json_encode($TZ); //store each line in the resulting array $result['records'][] = array("Contact" => $data[0], "Business Email" => $data[2], "Business Phone" => $data[3], "Time Zone" => $TZ); } //close the filehandle $db->close(); fclose($handle); } //return the json encoded result //echo json_encode($result); return; }