Skip to content

Instantly share code, notes, and snippets.

@jetonr
Created October 26, 2014 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jetonr/0a13b1331ab344d87cc2 to your computer and use it in GitHub Desktop.
Save jetonr/0a13b1331ab344d87cc2 to your computer and use it in GitHub Desktop.
echo array from csv file
function world_cities(){
$csv = array_map( 'str_getcsv', file( plugin_dir_path( __FILE__ ) . 'data/countries_bordered1.csv', FILE_SKIP_EMPTY_LINES ) );
$headers = $csv[0];
unset($csv[0]);
$rowsWithKeys = [];
foreach ( $csv as $row ) {
$newRow = [];
foreach ( $headers as $k => $key ) {
$newRow[$key] = explode( '|', $row[$k] );
}
$rowsWithKeys[] = $newRow;
}
return $rowsWithKeys;
}
$toto = world_cities();
foreach ( $toto as $entry ) {
$neigh = '';
$last_neighbour = '"'. array_pop( $entry['neighbors'] ) .'"';
foreach ( $entry['neighbors'] as $neighbour ) {
$neigh .= '"'.$neighbour.'", ';
}
echo '"'.$entry['country'][0].'"' . ' => ' . 'array(' . $neigh . $last_neighbour . '),' . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment