3v4l.org

run code in 300+ PHP versions simultaneously
<?php $aTest = array('apple', 'pineapple', 'banana', 'kiwi', 'pear', 'strawberry', '1234', '#qwerty'); $range = range('A','Z'); $range[] = '#'; $output = array(); foreach($range AS $letters){ foreach($aTest AS $fruit){ if(ucfirst($fruit[0]) == $letters){ $output[$letters][] = ucfirst($fruit); } } } foreach($output AS $letter => $fruits){ echo $letter . "<br/>--------<br/>\n"; sort($fruits); foreach($fruits AS $indFruit){ echo $indFruit . "<br/>\n"; } echo "<br/>\n"; } ?>

preferences:
41.72 ms | 402 KiB | 5 Q