<ul class="letters">
<?php foreach($data->real_letters as $key => $letter): ?>
<? $class = (array_key_exists($key, $second_array)) ? ' selected' : ''; ?>
<li><div class="letter <?= $class; ?>"><a href="#<?php echo $key; ?>"><?php echo $letter; ?></a></div></li>
<?php endforeach ?>
</ul>