$currentWard, "Candidate Name" => $candidateName, "Rating" => $rating, "Picture" => $picture ] ); } } fclose($handle); } else { error_log('Error opening input file'); exit(1); } if (empty($candidates)) { error_log("Failed to find any candidates"); exit(2); } if (($handle = fopen($outputFile, "w")) !== FALSE) { $headers = array( "Ward", "Candidate Name", "Rating", "Picture" ); if (fputcsv($handle, $headers) === FALSE) { error_log('Error writing headers to output file'); exit(3); } foreach ($candidates as $candidate) { $fields = array( $candidate["Ward"], $candidate["Candidate Name"], $candidate["Rating"], $candidate["Picture"] ); if (fputcsv($handle, $fields) === FALSE) { error_log('Error writing candidate to output file'); exit(3); } } } else { error_log('Error opening output file'); exit(1); } print("Data written to " . $outputFile); exit(0);