Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <!-- Link to CSS file (make your own) -->
- <link rel="stylesheet" href="css/master.css" type="text/css" media="screen" title="Sandbox" charset="utf-8">
- <title>Sorting names alphabetically</title>
- </head>
- <body>
- <h1>
- This demonstrates sorting an array of names alphabetically using PHP.
- </h1>
- <p>
- Here is the original list of names stored in the array: <br />
- <?php
- /**
- * As stated above, this will sort the names within an array alphabetically.
- *
- * @author: Shaun_B
- * @date: 2012-10-09
- * @var: $names of type array
- *
- **/
- // Array containing the names:
- $names = array( 'Shaun', 'Eric', 'Sarah', 'John', 'Barry', 'Dave', 'Zach', 'Helena' );
- // Calls our function, sending the array:
- showNames( $names );
- echo '<br />';
- sort( $names, SORT_ASC );
- echo 'And now this should be sorted alphabetically (A - Z): <br />';
- showNames( $names );
- // Our showNames function:
- function showNames( $array = null ) {
- foreach( $array as $name ) {
- echo $name;
- echo '<br />';
- }
- }
- ?>
- </p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement