Common PHP array sorting function induction, php array sorting

Source: Internet
Author: User

Common PHP array sorting function induction, php array sorting

Array sorting: elements in an array can be sorted in ascending or descending order by letters or numbers.
Common PHP array sorting functions
• Sort ()-sort arrays in ascending order
• Rsort ()-Sort arrays in descending order
• Asort ()-Sort joined arrays in ascending order based on values
• Ksort ()-Sort joined arrays in ascending order by key
• Arsort ()-Sort associated arrays in descending order based on values
• Krsort ()-Sort associated arrays in descending order by key

Example:

Sort ()-sort arrays in ascending order

Note: This function assigns a new key name to the cell in the array. The original key name is deleted.
If the call succeeds, true is returned. If the call succeeds, false is returned.
For example:

<? Php $ arr1 = array ('A', 'D', 'C', 'B'); $ arr2 = array (1, 3, 2, 4 ); print_r (sort ($ arr1 )? $ Arr1: "sorting failed"); echo "<br>"; print_r (sort ($ arr2 )? $ Arr2: "sorting failed ");

The running result is:
Array ([0] => a [1] => B [2] => c [3] => d)
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

The same is true for rsort () functions.

Asort ()-Sort joined arrays in ascending order by value

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.
If the call succeeds, TRUE is returned. Otherwise, FALSE is returned.

<? Php $ arr1 = array ('A', 'D', 'C', 'B'); $ arr2 = array (1, 3, 2, 4 ); print_r (asort ($ arr1 )? $ Arr1: "sorting failed"); echo "<br>"; print_r (asort ($ arr2 )? $ Arr2: "sorting failed ");

The running result is:
Array ([0] => a [3] => B [2] => c [1] => d)
Array ([0] => 1 [2] => 2 [1] => 3 [3] => 4)

The same is true for the arsort () function.

Ksort ()-Sort joined arrays in ascending order by key

The ksort () function sorts the array by key name and retains the original key for the array value.
If yes, TRUE is returned. Otherwise, FALSE is returned.

<? Php $ arr1 = array ('3' => 'third '', '2' => 'second'', '4' => 'Fourth ', '1' => '1',); print_r (ksort ($ arr1 )? $ Arr1: "sorting failed ");

The running result is:
Array ([1] => first [2] => second [3] => third [4] => fourth)

The same is true for the krsort () function.

Supplement-array_multisort () function

The array_multisort () function returns the sorting array. You can enter one or more arrays. The function first sorts the first array, followed by other arrays. If two or more values are the same, it sorts the next array.

Note: The string key name is retained, but the number key name is re-indexed. It starts from 0 and increments by 1.

Note: You can set the sorting order and sorting type parameters after each array. If no value is set, the default value is used for each array parameter.

<?php$a1=array("Dog","Dog","Cat");$a2=array(3, 2, 5);array_multisort($a1, SORT_ASC, $a2, SORT_DESC);print_r($a1);print_r($a2);

The running result is:
Array ([0] => Cat [1] => Dog [2] => Dog)
Array ([0] => 5 [1] => 3 [2] => 2)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.