Sorting Arrays-PHP


PHP supports a variety of ways to sort an array when 
I say sort, I am referring to an alphabetical sort if it is a string,
 and a numerical sort if it is a number. When sorting an array, 
you must keep in mind that an array consists of several pairs 
of keys and values. Thus, an array can be sorted based upon
 the values or the keys. Also, you can sort the values and keep 
the corresponding keys matched up or sort the values and 
have them receive new keys.
To sort the values, without regard to the keys, 
you use sort(). 
 To sort these values again without regard to the keys,
 in reverse order, you use rsort(). 
The syntax for every sorting function is like this:

function($Array);
So, sort() and rsort() are simply:
sort($Array);
rsort($Array);
 
To sort the values, while maintaining the correlation
 between the value and its key, you use asort(). 
To sort them in reverse, while maintaining the key
 correlation, you use arsort().
To sort by the keys, while still maintaining the correlation
 between the key and its value, you use ksort(). 
Conversely, krsort() will sort the keys in reverse.
Last, shuffle() randomly reorganizes the order of an array.

As an example of sorting arrays, you'll create a list of students
 and the grades they received on a test, then sort this list first
 by grade then by name.

Create the array:
 
$Grades = array(
"Richard"=>"95",
"Sherwood"=>"82",
"Toni"=>"98",
"Franz"=>"87",
"Melissa"=>"75",
"Roddy"=>"85"
);


 Print a caption and then print each element of 
the array using a loop.

print ("Originally, the array looks
 like this:<BR>");
for ($n = 0; $n < count($Grades);
 $n++) {
   $Line = each ($Grades);
   print ("$Line[key]'s grade is
 $Line[value].<BR>\n");
}


Sort the array in reverse order by values to determine 
who had the highest grade.

arsort($Grades);
 
Because you are determining who has the highest 
grade, you need to use arsort() instead of asort(). 
The latter, which sorts the array by numeric order, would 
order them 75, 82, 85, etc. and not the desired 98, 95, 87, etc.

Related Posts:
  • Php global variables Php Mail Php Array Php If else Php Variable This is a link Php Substrings Php Sessions Php global variables variables are automagically available in all contexts in function and global scopes. T… Read More
  • Php-Associative Arrays Arrays are another basic structure in programming languages. Arrays provide means for storing a fixed set (or collection) of the same datatype in a convenient way, making each element of your set indexable by using a uniqu… Read More
  • PHP mail function  sendmail cofiguration in php.ini file  If the smtp server you're trying to relay the    email to requires you to authenticate mail() will fail. <?php // ---------------- SEND MAIL FORM -------------… Read More
  • Php for-web developers   PHP MySQL Functions Free Hosting Requiring Cookies Web Application with PHP php -Mail Functions PHP Array Substrings PHP Comparison Operators for If/Else Statements Showing the Browser and I… Read More
  • PHP Tutorial PHP Functions Php tutorial - imagemagick php.ini Basics PHP Sessions Cookies Versus Sessions PHP Web-Related Variables PHP ERRORS maximum size of a file uploaded Php Image upload php file_get_conte… Read More
  • Advanced PHP PHP's strength lies in its huge library of built-in functions, which allows even a novice user  to perform very complicated tasks without having to install new libraries or worry about low-level details, as is often th… Read More
  • Php Form Example Since you'll need a place for the user to enter a search query, let's begin by building a form to handle the user's input. Every form must have these basic components:The submission type defined with the method keywordOne… Read More
  • Sends a message via mail function in PHP mail function allows you to send email directly from a PHP script. recipient can be  either a single email address or a comma-delimited list of addresses.  If you want to set extra headers—for instance, in order … Read More
  • what is IMAP IMAP, fully documented in RFC 3501, was designed to provide a robust, mobile mail delivery and access mechanism. For more detail on the protocol and how it functions on the network layer, or for additional information on… Read More
  • mysql_query-executes query mysql_query function  executes query on the default database, set using mysql_select_db() or by a previous query using mysql_db_query(), on the MySQL server connection referenced by connection . If no connection … Read More
  • PHP's variable-related functions PHP's variable-related functions are a key part of the language. Skilled programmers rely on them extensively to build robust code that uses type-checking. Functions like var_dump() and print_r() are also invaluable w… Read More
  • Database Functions-PHP MySQL PostgreSQL MS SQL (Microsoft) Chances are good that you will have at least one of these databases available to you (very good since MySQL and PostgreSQL are available for free download). There are four… Read More
  • Learn PhP By Code PHP - Echo example            <?php              $myString = "Hi! This is a test";         … Read More
  • Advanced Database Job PHP PHP supports the following databases in one form or another: MySQL— www.mysql.com mSQL— www.hughes.com.au MS SQL (Microsoft SQL server; on Win32 systems only) filePro (Read only)— www.fptech.com Informix— (fr… Read More
  • Abstract data type In programming, a data set defined by the programmer in terms of the information it can contain and the operations that can be performed with it. An abstract data type is more generalized than a data type constr… Read More