Advertisement
lulzimg

Untitled

Mar 2nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. <style>
  2. .letter {
  3. text-transform: uppercase;
  4. }
  5. </style>
  6. <?php
  7. error_reporting(0);
  8. /*
  9. Challenge: An array that contains names, find all names that start for ex with 'a',
  10. split them in sections and display all the name that start with 'a' on its own section
  11. if there is not a section ex 'c' don't display it
  12. */
  13. $names = ['Zenel', 'Burim', 'Arlind', 'Besart', 'Kastriot', 'Zana', 'Ajete', 'Vleran', 'Arben', 'Jeton', 'Jeta', 'Aulonë', 'Astrit'];
  14. $sortedNames = [];
  15. $lettersAlphabet = range('a', 'z');
  16. $letters = [];
  17.  
  18. $j = -1;
  19.  
  20. sort($names);
  21. foreach($names as $key => $n) {
  22. $firstLetter = strtolower(substr($n, 0, 1));
  23.  
  24. $sortedNames[$firstLetter] = $n;
  25.  
  26. $letters[] = $firstLetter;
  27.  
  28. if ($letters[$j] != $firstLetter) {
  29. $letters[$j] = $firstLetter;
  30. echo "Names starting with: <strong class='letter'>{$letters[$j]}</strong> <br />";
  31. }
  32. echo "{$sortedNames[$firstLetter]} <br />";
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement