Advertisement
markuszeller

Sorting with headers

Jul 18th, 2021 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.41 KB | None | 0 0
  1. <?php
  2. $animals = file('https://gist.githubusercontent.com/atduskgreg/3cf8ef48cb0d29cf151bedad81553a54/raw/82f142562cf50b0f6fb8010f890b2f934093553e/animals.txt', FILE_SKIP_EMPTY_LINES);
  3. sort($animals);
  4.  
  5. $last = '';
  6. foreach ($animals as $animal) {
  7.     $current = substr($animal, 0 ,1);
  8.     if($current !== $last) {
  9.         echo $current, ' ---<br>', PHP_EOL;
  10.         $last = $current;
  11.     }
  12.     echo $animal;
  13. }
  14.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement