Another technique not yet mentioned is to use preg_replace() -- which will happily iterate an array of strings -- although, obviously this is not a task that actually demands a regex pattern. (Demo)
$links = array('london','new york','paris');
var_export(
preg_replace('/.+/', '<a href="#$0">$0</a>', $links)
);
Otherwise, you can go that extra step toward generating encoded urls by calling urlencode() on the city string which is used as the href value.
var_export(
array_map(
fn($city) => sprintf('<a href="#%s">%s</a>', urlencode($city), $city),
$links
)
);
foreachloops? Oh, you just answered as I posted!