An example of an array sort function commonly used in PHP

Source: Internet
Author: User
Tags array sort numeric value sorts
$fruits =array (
"D" => "Lemon",
"A" => "orange",
"B" => "banana",
"C" => "Apple"); Define an array
Arsort ($fruits); Reverse-Sort the array
foreach ($fruits as $key => $val)//Cyclic output the key value pairs of the array after sorting
{
echo "$key = $valn"; Output content
}

/*
Arsort (Array,sorttype)
The Arsort () function reverses the array and maintains an indexed relationship. It is primarily used to sort the associative arrays that are important to the order of the cells.

The optional second parameter contains an additional sort identity.

Returns true if successful, otherwise returns false.

*/

$fruits =array (
"D" => "Lemon",
"A" => "orange",
"B" => "banana",
"C" => "Apple"); Define an array
Asort ($fruits); To sort a set of arrays
foreach ($fruits as $key => $val)//Cyclic output the key value pairs of the array after sorting
{
echo "$key = $valn"; Output content
}

/*
Definitions and usage
The Asort () function sorts the array and maintains an indexed relationship. It is primarily used to sort the associative arrays that are important to the order of the cells.

The optional second parameter contains an additional sort identity.

Returns true if successful, otherwise returns false.

Grammar
Asort (Array,sorttype)

*/

$fruits =array (
"D" => "Lemon",
"A" => "orange",
"B" => "banana",
"C" => "Apple"); Define an array
Krsort ($fruits); To reverse sort the array by key name
foreach ($fruits as $key => $val)//Cyclic output the key value pairs of the array after sorting
{
echo "$key = $valn"; Output content
}

/*
Definitions and usage
The Krsort () function sorts the array in reverse order, preserving the original key for the numeric value.

The optional second parameter contains the additional sort flags.

If successful, returns TRUE, otherwise returns false.

Grammar

Krsort (Array,sorttype)
*/
$fruits =array (
"D" => "Lemon",
"A" => "orange",
"B" => "banana",
"C" => "Apple"); Define an array
Ksort ($fruits); Arrays are sorted by key name
foreach ($fruits as $key => $val)//Cyclic output the key value pairs of the array after sorting
{
echo "$key = $valn"; Output content
}

/*
Ksort Array Key Name ranking

The Ksort () function sorts by an array of key names, preserving the original key for the array value.

The optional second parameter contains the additional sort flags.

If successful, returns TRUE, otherwise returns false.


This value is a new addition to the PHP tutorials 4.4.0 and 5.0.2. Before PHP 6, the locale of the system was used and can be changed with setlocale (). From PHP 6, you must use the I18n_loc_set_default () function.


For more details please see: http://www.111cn.net/phper/24/b87e0d97494df8bc5a426c1a582168b8.htm
*/

Define two arrays at the same time

$a 1= $a 2=array (' img0.png ', ' img8.png ', ' img10.png ');
Sort ($a 1); Sort by array 1
echo "Standard SORTINGN";
Print_r ($a 1); Output a normal sort
Natcasesort ($a 2); Natural sorting of arrays and case-insensitive sorting
echo "nnatural order sorting (case-insensitive) n";
Print_r ($a 2);

/*
The Natcasesort () function sorts the elements in a given array with a case-insensitive natural order algorithm.

The Natcasesort () function implements a "natural sort", the sort method of numbers from 1 to 9, the sorting method of letters from A to Z, the short precedence, and the function is case-insensitive. The index of an array remains associated with the cell value.

If successful, the function returns True, otherwise it returns false.

Grammar

Natcasesort (Array)
*/
$a 1= $a 2=array (' img0.png ', ' img8.png ', ' img10.png ');
Sort ($a 1); Sort by array 1
echo "Standard SORTINGN";
Print_r ($a 1); Output a normal sort
Natsort ($a 2); sorting arrays by natural sorting method
echo "nnatural order Sortingn";
Print_r ($a 2); Outputs the result of the second sort

/*
sorting arrays by natural sorting method
*/

$fruits =array ("Lemon", "orange", "banana", "apple"); Define an array
Rsort ($fruits); Reverse-Sort the array
foreach ($fruits as $key => $val)//Cyclic output the key value pairs after the sort of the array
{
echo "$key = $valn"; Output key value pairs
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.