function load_names_data($file_name) { if (!file_exists($file_name)) { log_warning(0, "Couldn't find file '{$file_name}'"); return; } $csv_data = read_csv_file($file_name); $output = array(); foreach ($csv_data as $row) { $names_string = $row['names']; $names = explode('|', $names_string); foreach ($names as $name) { $name = normalize_name($name); $name_words = explode(' ', $name); $name_words = array_reverse($name_words); $first_word = $name_words[0]; if (!isset($output[$first_word])) { $output[$first_word] = array(); } $preceding_words = array_slice($name_words, 1); $output_row = array('name' => $name, 'preceding_words' => $preceding_words); foreach ($row as $key => $value) { if ($key !== 'name') { $output_row[$key] = $value; } } $output[$first_word][] = $output_row; } } return $output; }
$row[$column_name] = null; } $column_index += 1; } $result[] = $row; } fclose($file_handle); return $result; } function write_csv_file($file_name, $data, $seperator = ',') { $file_handle = fopen($file_name, "w") or die("Couldn't open {$file_name}\n"); $first_row = $data[0]; $column_names = array_keys($first_row); fputcsv($file_handle, $column_names, $seperator); foreach ($data as $row) { $current_parts = array_values($row); fputcsv($file_handle, $current_parts, $seperator); } fclose($file_handle); } $cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The file to read the PlaceIQ format data from - if unset, will write to stdout', 'default' => 'php://stdin'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the converted CSV data to - if unset, will write to stdout', 'default' => 'php://stdout')); $options = cliargs_get_options($cliargs); $input_file = $options['inputfile']; $output_file = $options['outputfile']; $input_data = read_csv_file($input_file, "\t"); $output_data = array(); foreach ($input_data as $row) { $output_data[] = array('latitude' => $row['latitude'], 'longitude' => $row['longitude'], 'description' => $row['name']); } write_csv_file($output_file, $output_data, ',');