Created
October 26, 2014 23:53
-
-
Save jetonr/0a13b1331ab344d87cc2 to your computer and use it in GitHub Desktop.
echo array from csv file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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