I have a big piece of php code that builds a dropdown menu and I want to minimize it by loading the select options from a text file. How to do this in the right way with php?
An example of original code:
$field['options'] = array(
array('value' => 'Anenii Noi', 'text' => 'Anenii Noi', 'depth' => 0),
array('value' => 'Bălţi', 'text' => 'Bălţi', 'depth' => 0),
array('value' => 'Basarabeasca', 'text' => 'Basarabeasca', 'depth' => 0),
....
array('value' => 'Ungheni', 'text' => 'Ungheni', 'depth' => 0)
);
And this is what I try to do, but without success:
// read a text file with select options
// where each option is a seperate line
$file_array = str_replace("\n","", file($file_path));
// build the list of arrays with select options
foreach ($file_array as $location)
$options .= "array('value' => '" . $location . "', 'text' => '" . $location . "', 'depth' => 0),";
foreach($form['field'] as $k => $field) {
// replace the big list of arrays with $options variable
$field['options'] = array($options);
$form['field'][$k] = $field;
break;
}