Skip to content

Instantly share code, notes, and snippets.

@caendesilva
Created October 11, 2024 11:19
Show Gist options
  • Save caendesilva/5fae6bcf1d4b23766b8786a01603e07b to your computer and use it in GitHub Desktop.
Save caendesilva/5fae6bcf1d4b23766b8786a01603e07b to your computer and use it in GitHub Desktop.
<?php
// Function to extract city_ascii and country names
function extractLocationParts($filename) {
// Initialize an empty array to hold the merged location strings
$locations = [];
// Open the CSV file
if (($handle = fopen($filename, 'r')) !== false) {
// Get the header line
$header = fgetcsv($handle);
// Find the indices for city_ascii and country
$cityIndex = array_search('city_ascii', $header);
$countryIndex = array_search('country', $header);
// Read each row of the CSV file
while (($row = fgetcsv($handle)) !== false) {
// Get the city_ascii and country values
$city = $row[$cityIndex] ?? '';
$country = $row[$countryIndex] ?? '';
// Merge city and country
$mergedLocation = trim("$city $country");
if (!empty($mergedLocation)) {
$locations[] = $mergedLocation;
}
}
// Close the file handle
fclose($handle);
} else {
echo "Error opening the file.";
return [];
}
// Initialize an empty array for the final flat array of parts
$flatArray = [];
// Explode each location string by spaces and merge into a flat array
foreach ($locations as $location) {
$parts = explode(' ', $location);
$flatArray = array_merge($flatArray, $parts);
}
// Unique array
$result = array_unique($flatArray);
return $result;
}
// Usage example
$filename = __DIR__.'/worldcities.csv';
$locationParts = extractLocationParts($filename);
// Create a txt output file where we write 10 parts per line
$fp = fopen('output.txt', 'w');
$count = 0;
foreach ($locationParts as $key => $part) {
$buffer = $part . ' | ';
if ($count === 10) {
fwrite($fp, $buffer . PHP_EOL);
$count = 0;
} else {
fwrite($fp, $buffer);
$count++;
}
}
// Output the flat array of location parts
print_r($locationParts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment