$cities = ['Moscow', 'London', 'NY', 'LA'];
sort($cities);
$citiesByLetters = [];
foreach ($cities as $city) {
$citiesByLetters[$city[0]][] = $city;
}
foreach ($citiesByLetters as $letter => $cities) {
echo '<div id="letter-'.$letter.'">';
foreach ($cities as $city) {
echo '<div class="city-el">'.$city.'</div>';
}
echo '</div>';
}