Skip to content

Instantly share code, notes, and snippets.

@pjaudiomv
Last active January 26, 2019 14:20
Show Gist options
  • Save pjaudiomv/eab21c03f3aa04c4fa96e3b42ac39758 to your computer and use it in GitHub Desktop.
Save pjaudiomv/eab21c03f3aa04c4fa96e3b42ac39758 to your computer and use it in GitHub Desktop.
<?php
// Get all Counties for the following state, AL, FL, GA, NC, SC
$al_file = "http://www2.census.gov/geo/docs/reference/codes/files/st01_al_cou.txt";
$fl_file = "https://www2.census.gov/geo/docs/reference/codes/files/st12_fl_cou.txt";
$ga_file = "https://www2.census.gov/geo/docs/reference/codes/files/st13_ga_cou.txt";
$nc_file = "https://www2.census.gov/geo/docs/reference/codes/files/st37_nc_cou.txt";
$sc_file = "https://www2.census.gov/geo/docs/reference/codes/files/st45_sc_cou.txt";
$states = array($al_file, $fl_file, $ga_file, $nc_file, $sc_file);
$allCounties = array();
foreach ($states as $state) {
$csv = array_map('str_getcsv', file($state));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine(array('STATE','STATEFP','COUNTYFP','COUNTYNAME','CLASSFP'), $a);
});
$allCounties[] = array_column($csv, 'COUNTYNAME');
}
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($allCounties));
$new_arr = array();
foreach($iter as $v) {
$new_arr[]=$v;
}
$finalCountyArray = array();
foreach ($new_arr as $arr) {
$finalCountyArray[] = str_replace(' County', '', trim(ucwords(strtolower($arr))));
}
array_push($finalCountyArray,"Greensville","Giles");
// Greensville - VA
// Giles - TN
sort($finalCountyArray);
$uniqueCounties = array_unique($finalCountyArray);
//echo "\n" . count($uniqueCounties) . "\n\n"; // Count
$allCounties = '';
foreach ($uniqueCounties as $uniqueCounty) {
$allCounties .= "'" . addslashes($uniqueCounty) . "',"; //add slashes just in case
}
$allCounties = rtrim($allCounties,","); // trim last comma ,
echo '$meeting_counties_and_sub_provinces = array(';
echo $allCounties;
echo ');';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment