Ваш цикл foreach
, который определяет первую букву заголовка, находится вне цикла for
, который фактически печатает его. Поэтому вы всегда будете выводить последнюю букву цикла foreach
.
Переместите эту часть в цикл for
, что-то вроде
$limit = 3000;
$previous = null;
$count_firstletters = 0;
for ($x = 0; $x < $limit; $x++) {
$firstLetter = substr($feed[$x]['title'], 0, 1); // Getting the first letter from the Title you're going to print
if($previous !== $firstLetter) { // If the first letter is different from the previous one then output the letter and start the UL
if($count_firstletters != 0) {
echo '</ul>'; // Closing the previously open UL only if it's not the first time
}
echo '<p>'.$firstLetter.'</p>';
echo '<ul style="list-style-type: none;>"';
$previous = $firstLetter;
$count_firstletters ++;
}
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
echo '<li>';
echo '<a href="" title="'.$title.'" target="_blank">'.$title.'</a>';
echo '</li>';
}
echo '</ul>'; // Close the last UL