Do you need a city code for each city in the world? Search no more …
I did used a lot the country code the 2 or 3 letters version in the apps and websites I had worked, and did not care about city codes until now.
After a short research I did found that you can have a similar list of city codes (not the phone prefix) for cities. There is IATA airport/city codes however this have unique codes for each cities that have a airport. (not completed). The list can be downloaded from official IATA page (not free)
And there is a UN-LOCODE named United Nations Code for Trade and Transport Locations maintained by the UN. This is a complete list of code for each cities available as a free download in CSV form from Github and Open Data