Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to convert a csv file into Sugar php dropdowns
<?php
// example dropdowns.csv content
// "ddname","key1","value1"
// "ddname","key2","value2"
$fileName = 'dropdowns.csv';
if (file_exists($fileName)) {
$file = fopen($fileName, 'r');
$dd = [];
while (($line = fgetcsv($file)) !== FALSE) {
if (count($line) === 3) {
// clean ddname
$ddname = preg_replace('/[^a-z0-9]/', '_', strtolower($line[0]));
// clean keys
$key = preg_replace('/[^a-z0-9]/', '_', strtolower($line[1]));
$val = $line[2];
echo "\$app_list_strings['" . $ddname . "']['" . $key . "'] = '" . $val . "';" . PHP_EOL;
} else {
echo 'The file format is invalid, it should have 3 CSV columns for each row: "ddname","key","value", provided instead ' . print_r($line, true) . PHP_EOL;
}
}
fclose($file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.