The difference between sort,ksort,asort

Source: Internet
Author: User
Tags locale setting

sort--to sort the Val of an array

ksort--to sort the key values of an array

asort--the array, the corresponding relationship between the key and the value is unchanged

1. Sort array

The format is as follows: bool Sort (array & $array [, int $sort _flags])

After using this function, the unit will be rescheduled from lowest to highest.

Returns a Boolean

$sort _flags This parameter is added in PhP4, you can change the ordering behavior with the following values

Sort type tag:

sort_regular-Normal comparison unit (does not change type)

The sort_numeric-unit is compared as a number.

The sort_string-unit is compared as a string.

sort_locale_string-the cell as a string comparison based on the current locale setting. php4.4.0 and 5.0.2 new addition. In

Before PHP6, the system's locale was used, which can be changed with setlocale (). Starting from PHP6, the I18n_loc_set_default () function must be used

Illustrate the use of sort

$array = Array (' 23 ', ' 96 ', ' 12 ', ' 50 ', ' 3 ');

Sort ($array);

foreach ($array as $key = = $val) {

echo "array[". $key. "] = ". $val." /n ";

}

2. ksort-array sorted by key name

The format is as follows: BOOL Ksort (Array & $array [, int $sort _flags])

The array is sorted by key name, preserving the association of the key name to the data.

Returns a Boolean

$sort _flags parameters are used like sort

Illustrate the use of ksort

$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");

Ksort ($fruits);

foreach ($fruits as $key = = $val) {

echo "$key = $val/n";

}

Output

A = Orange

b = Banana

c = Apple

D = Lemon

3. asort-the array and keep the index relationship

The format is as follows: BOOL Asort (Array & $array [, int $sort _flags])

This function sorts the arrays, the index of the array remains, and the cell is associated. Used primarily to sort the associative arrays that are important for the cell order

Returns a Boolean value

$sort _flags parameters are used like sort

Illustrate the use of ksort

$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");

Ksort ($fruits);

foreach ($fruits as $key = = $val) {

echo "$key = $val/n";

}

Output

c = Apple

b = Banana

D = Lemon

A = Orange

The difference between sort,ksort,asort

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.