Advertisement
Shaun_B

Sorting an array alphabetically using PHP.

Oct 9th, 2012
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.     <head>
  5.         <!-- Link to CSS file (make your own) -->
  6.         <link rel="stylesheet" href="css/master.css" type="text/css" media="screen" title="Sandbox" charset="utf-8">
  7.         <title>Sorting names alphabetically</title>
  8.     </head>
  9.     <body>
  10.         <h1>
  11.             This demonstrates sorting an array of names alphabetically using PHP.
  12.         </h1>
  13.         <p>
  14.         Here is the original list of names stored in the array: <br />
  15.         <?php
  16.         /**
  17.          * As stated above, this will sort the names within an array alphabetically.
  18.          *
  19.          * @author:     Shaun_B
  20.          * @date:       2012-10-09
  21.          * @var:        $names of type array
  22.          *
  23.          **/
  24.        
  25.         // Array containing the names:
  26.         $names = array( 'Shaun', 'Eric', 'Sarah', 'John', 'Barry', 'Dave', 'Zach', 'Helena' );
  27.        
  28.         // Calls our function, sending the array:
  29.         showNames( $names );
  30.         echo '<br />';
  31.         sort( $names, SORT_ASC );
  32.         echo    'And now this should be sorted alphabetically (A - Z): <br />';
  33.         showNames( $names );
  34.         // Our showNames function:
  35.         function showNames( $array = null ) {
  36.             foreach( $array as $name ) {
  37.                 echo $name;
  38.                 echo '<br />';
  39.             }
  40.         }
  41.         ?>
  42.         </p>
  43.     </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement