Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <style>
- .letter {
- text-transform: uppercase;
- }
- </style>
- <?php
- error_reporting(0);
- /*
- Challenge: An array that contains names, find all names that start for ex with 'a',
- split them in sections and display all the name that start with 'a' on its own section
- if there is not a section ex 'c' don't display it
- */
- $names = ['Zenel', 'Burim', 'Arlind', 'Besart', 'Kastriot', 'Zana', 'Ajete', 'Vleran', 'Arben', 'Jeton', 'Jeta', 'Aulonë', 'Astrit'];
- $sortedNames = [];
- $lettersAlphabet = range('a', 'z');
- $letters = [];
- $j = -1;
- sort($names);
- foreach($names as $key => $n) {
- $firstLetter = strtolower(substr($n, 0, 1));
- $sortedNames[$firstLetter] = $n;
- $letters[] = $firstLetter;
- if ($letters[$j] != $firstLetter) {
- $letters[$j] = $firstLetter;
- echo "Names starting with: <strong class='letter'>{$letters[$j]}</strong> <br />";
- }
- echo "{$sortedNames[$firstLetter]} <br />";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement