Array to CSV Class Documentation
- Including
- Displaying CSV code
- Saving CSV code as file
- Custom seperator
- Create 2 CSV files at the same time
Including
Include class.ArrayToCSV.php on the page where you want to use the class.
include("class.ArrayToCSV.php");
Displaying CSV code
Your PHP code
$csv = new ArrayToCSV;
// Generating a new CSV file.
$csv->new_file();
// Adding lines to the CSV file.
$csv->line(Array("Username.","e-mail","newsletter","facebook","website"));
$csv->line(Array("john.smith","j.smith@gmail.com","yes","JohnnnSmith","john-smith.com"));
$csv->line(Array("william.smith","w.smith@gmail.com","no","Mr.William.Smith","william-s.com"));
// Display the CSV code
echo $csv->output();
Save CSV code as file
Your PHP code
$csv = new ArrayToCSV;
// Generating a new CSV file.
$csv->new_file();
// Adding lines to the CSV file.
$csv->line(Array("Username.","e-mail","newsletter","facebook","website"));
$csv->line(Array("john.smith","j.smith@gmail.com","yes","JohnnnSmith","john-smith.com"));
$csv->line(Array("william.smith","w.smith@gmail.com","no","Mr.William.Smith","william-s.com"));
// Save CSV code as file
$csv->save("registrations.csv");
Custom seperator
The default seperator is ;, this is the most used seperator.
Your PHP code
$csv = new ArrayToCSV;
// Generating a new CSV file.
$csv->new_file();
// Choose new seperator.
$csv->seperator(";");
// Adding lines to the CSV file.
$csv->line(Array("Username.","e-mail","newsletter","facebook","website"));
$csv->line(Array("john.smith","j.smith@gmail.com","yes","JohnnnSmith","john-smith.com"));
$csv->line(Array("william.smith","w.smith@gmail.com","no","Mr.William.Smith","william-s.com"));
// Save CSV code as file
$csv->save("registrations.csv");
Create 2 CSV files at the same time
You can create a second CSV file by using $csv->new_file(); . See the code below for an example.
Your PHP code
$csv = new ArrayToCSV;
// Generating a new CSV file.
$csv->new_file();
// Adding lines to the CSV file.
$csv->line(Array("Username.","e-mail","newsletter","facebook","website"));
$csv->line(Array("john.smith","j.smith@gmail.com","yes","JohnnnSmith","john-smith.com"));
$csv->line(Array("william.smith","w.smith@gmail.com","no","Mr.William.Smith","william-s.com"));
// Save CSV code as file
$csv->save("registrations.csv");
// Generating second CSV file.
$csv->new_file();
// Adding lines to the CSV file.
$csv->line(Array("Username.","e-mail","newsletter","facebook","website"));
$csv->line(Array("john.smith","j.smith@gmail.com","yes","JohnnnSmith","john-smith.com"));
$csv->line(Array("william.smith","w.smith@gmail.com","no","Mr.William.Smith","william-s.com"));
// Save CSV code as file
$csv->save("registrations2.csv");