<div>
{% for city in cities %}
<div class="cities_city">
<a class="cities_city-name" data-id="{{ city.fias_id }}">
{{ city.name }}
</a>
</div>
{% endfor %}
</div>
$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>';
}