How do I sort the PHP array?

Source: Internet
Author: User
Tags locale setting
How do I sort the PHP array?

We are in the daily PHP array development, many projects do not have to sort the array, then there are several methods of ordering in PHP arrays, namely: Asort () function and Ksort () function, today one by one to introduce you!

First method: Asort ()

The Asort () function is to sort the array and keep the index relationship

The syntax format is as follows:

BOOL Sort (array & $array [, int $sort _flags = sort_regular])

Sort type tag:

    • Sort_regular-Normal comparison unit (does not change type)

    • Sort_numeric-units are compared as numbers

    • Sort_string-units are compared as strings

    • Sort_locale_string-the unit is treated as a string comparison according to the current locale setting, and can be changed with setlocale ().

    • Sort_natural-and Natsort () are similar to sorting strings in "natural order" for each unit. New in PHP 5.4.0.

    • Sort_flag_case-capable of merging with sort_string or sort_natural (or bitwise operations), case-insensitive sorting strings.

Note: Sort is mostly used to sort a numeric index array, and if an associative array is sorted in sort, then the keys of the array are lost. In addition UTF8 Chinese cannot be sorted.

Here is a small example of the sort function, with the following code:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $arr = [' g ', ' h ', ' d ', ' a ', ' Z ', ' f ', ' B ', ' C ', ' e ', ' a ', ' F '];p Rint_r ($arr); sort ($arr); From A-Z, a-Z, numbers from small to large to  sort Print_r ($arr);? >

If the value is numeric using the sort function, what is the case, see the following code example:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $arr 2 = [8,1,2,10,3,4,5];p Rint_r ($arr 2); sort ($arr 2); If the value is a number of children, from small to large, to sort Print_r ($arr 2);? >

The output is:

From the above example, we can see that if the value of the array is a number, using the sort () function, it will be sorted from small to large!

the second method is to Associative array Ascending: Asort () and Ksort ()

The Asort () function is to sort the array and keep the index relationship.

Since associative arrays cannot be sorted using sort, what functions should be used? We recommend using Asort, which is the same as sort, but after sorting, the keys can still be preserved

The syntax format is as follows:

BOOL Asort (array & $array [, int $sort _flags = sort_regular])

The following is a small instance of the Asort function, as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 ");//$arr = Array (" One "and" + "," one "and" + "," + "=" tutorial "), $arr [' one '] = +, $arr [' one '] = 20;$ arr[' three '] = 3;asort ($arr);     Asort sort, the key value will retain Print_r ($arr); >

The output of the result is;

Attention:

Associative arrays are made up of keys and values, so what exactly is asort based on?

We can prove that Asort is actually sorted using the values of the array, so if we want to sort the keys, we can use Ksort.

The Ksort () function is an array of arrays sorted by key name.

The syntax format is as follows:

BOOL Ksort (array & $array [, int $sort _flags = sort_regular])

The array is sorted by key name, preserving the association of the key name to the data. This function is mainly used for associative arrays.

The following is a small instance of the Ksort function, as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 ");//$arr = Array (" One "and" + "," one "and" + "," + "=" tutorial "), $arr [' one '] = +, $arr [' one '] = 20;$ arr[' three '] = 3;ksort ($arr);    Use the key value to sort the array print_r ($arr);? >

The output is:

In this article we introduce the ascending of the PHP array, then we introduce the reverse of the array in the next article, the reverse also has three functions to implement, the next article I will detail the countdown of three functions, see the "Reverse of PHP array"!

"Related tutorials Recommended"

1. Related topics: "PHP array"

2. Related Video Courses recommended:

array sorted by value: sort () Forward and rsort () reverse sort function

Array Key Name sort: Ksort () Ascending and krsort () descending function

Related Article

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.