I have an array of letters that are used to create a header and sort an array of listItems from my database. What i have right now is:
$alph = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
foreach($alph as $data) {
echo "<h3 id=" . $data .">" . $data . "</h3><br />";
foreach($list as $listItem) {
if(strtolower(substr($listItem->title, 0, 1)) === $data) {
echo $listItem;
}
}
}
This works great but I feel there may be improvements that can be made to increase speed.
Solution that is O(n) i think, but cheats with php hash arrays: