Php array sorting function example-PHP source code

Source: Internet
Author: User
The following article describes how to use our commonly used array sorting functions and examples, including arsortasortkrsortnatcasesortrsort. The following article describes how to use our commonly used array sorting functions and examples, including arsort asort krsort natcasesort rsort.

Script ec (2); script

$ Fruits = array (
"D" => "lemon ",
"A" => "orange ",
"B" => "banana ",
"C" => "apple"); // defines an array
Arsort ($ fruits); // sorts the array in reverse order.
Foreach ($ fruits as $ key => $ val) // cyclically outputs the sorted array key-Value Pair
{
Echo "$ key = $ valn"; // output content
}

/*
Arsort (array, sorttype)
The arsort () function sorts arrays in reverse order and maintains the index relationship. It is mainly used to sort the arrays that are very important to the unit order.

The second optional parameter contains an additional sorting identifier.

If the call succeeds, true is returned. Otherwise, false is returned.

*/

$ Fruits = array (
"D" => "lemon ",
"A" => "orange ",
"B" => "banana ",
"C" => "apple"); // defines an array
Asort ($ fruits); // sorts the Array
Foreach ($ fruits as $ key => $ val) // cyclically outputs the sorted array key-Value Pair
{
Echo "$ key = $ valn"; // output content
}

/*
Definition and usage
The asort () function sorts arrays and maintains the index relationship. It is mainly used to sort the arrays that are very important to the unit order.

The second optional parameter contains an additional sorting identifier.

If the call succeeds, true is returned. Otherwise, false is returned.

Syntax
Asort (array, sorttype)

*/

$ Fruits = array (
"D" => "lemon ",
"A" => "orange ",
"B" => "banana ",
"C" => "apple"); // defines an array
Krsort ($ fruits); // sort the array in reverse order by key name
Foreach ($ fruits as $ key => $ val) // cyclically outputs the sorted array key-Value Pair
{
Echo "$ key = $ valn"; // output content
}

/*
Definition and usage
The krsort () function sorts the array by key in reverse order to reserve the original key for the array value.

The second optional parameter contains an additional sorting flag.

If yes, true is returned. Otherwise, false is returned.

Syntax

Krsort (array, sorttype)
*/
$ Fruits = array (
"D" => "lemon ",
"A" => "orange ",
"B" => "banana ",
"C" => "apple"); // defines an array
Ksort ($ fruits); // sort the array by key name
Foreach ($ fruits as $ key => $ val) // cyclically outputs the sorted array key-Value Pair
{
Echo "$ key = $ valn"; // output content
}

/*
Ranking of key names in the ksort Array

The ksort () function sorts the array by key name and retains the original key for the array value.

The second optional parameter contains an additional sorting flag.

If yes, true is returned. Otherwise, false is returned.


This value is newly added to php tutorials 4.4.0 and 5.0.2. Before php 6, you can use setlocale () to change the region settings of the system. From php 6, you must use the i18n_loc_set_default () function.


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

// Define two arrays at the same time

When a11_a21_array('img0.png', 'img8.png', 'img10.png ');
Sort ($ a1); // sorts array 1
Echo "standard sortingn ";
Print_r ($ a1); // The output is sorted normally.
Natcasesort ($ a2); // sort arrays in a case-insensitive manner.
Echo "nnatural order sorting (case-insensitive) n ";
Print_r ($ a2 );

/*
The natcasesort () function uses a case-insensitive natural sequence algorithm to sort the elements in a given array.

The natcasesort () function implements "natural sorting", that is, the sorting method for numbers from 1 to 9. The sorting method for letters from a to z takes precedence over short ones. This function is case insensitive. The index of the array is associated with the unit value.

If the call succeeds, the function returns true; otherwise, the function returns false.

Syntax

Natcasesort (array)
*/
When a11_a21_array('img0.png', 'img8.png', 'img10.png ');
Sort ($ a1); // sorts array 1
Echo "standard sortingn ";
Print_r ($ a1); // The output is sorted normally.
Natsort ($ a2); // sort the arrays by natural sorting.
Echo "nnatural order sortingn ";
Print_r ($ a2); // outputs the result of the second sort.

/*
Sort arrays by natural sorting
*/

$ Fruits = array ("lemon", "orange", "banana", "apple"); // defines an array
Rsort ($ fruits); // sorts the array in reverse order.
Foreach ($ fruits as $ key => $ val) // cyclically output the sorted key-value pair 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.