Are there any PHP libraries or functions that can simplify the process of displaying headers for each new initial letter in an alphabetically sorted array?

When displaying headers for each new initial letter in an alphabetically sorted array, we can use the PHP function `strtoupper()` to convert the first letter of each element to uppercase and compare it with the previous element to determine if a new header is needed. We can then use an if statement to check if the current element's first letter is different from the previous element's first letter, and if so, display a header for that letter.

<?php
$array = array("apple", "banana", "cherry", "grape", "kiwi", "orange", "pear");
$prev_letter = '';

foreach ($array as $element) {
    $first_letter = strtoupper(substr($element, 0, 1));

    if ($first_letter != $prev_letter) {
        echo "<h2>{$first_letter}</h2>";
        $prev_letter = $first_letter;
    }

    echo $element . "<br>";
}
?>