PHP Array Sorting summary

Source: Internet
Author: User

Array sorting
the elements in the array can be sorted in ascending or descending order, either in alphabetical or numerical sequence.

Common PHP Array sorting functions

    • Sort ()-sorts an array in ascending order
    • Rsort ()-sorts the array in descending order
    • Asort ()-sorts associative arrays in ascending order based on values
    • Ksort ()-Sorts the associative array in ascending order, based on the key
    • Arsort ()-sorts associative arrays in descending order by value
    • Krsort ()-Sorts the associative array in descending order, based on the key

Example:

Sort ()-sorts an array in ascending order

Note: This function assigns the new key name to the cells in the array. The original key name is deleted.
Returns failure if successful true returns false
For example:

<?php   $arr 1  = array  ( ' a ' ,  ' d ' ,  ' C ' ,  ' B '  ');  $arr 2  = array  (1 , 3 , 2 , 4 );p rint_r (sort ( $arr 1 )?  $arr 1 :  "sort Failed" ); echo  ;p rint_r (sort ( $arr 2 )?  $arr 2 :  "sort Failed" );  

The result of the operation is:
Array ([0] = a [1] = b [2] = = c [3] = + D)
Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4)

Rsort () function similarly

Asort ()-sorts associative arrays in ascending order based on values

the Asort () function sorts the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order.
Returns TRUE if successful, otherwise FALSE.

<?php   $arr 1  = array  ( ' a ' ,  ' d ' ,  ' C ' ,  ' B '  ');  $arr 2  = array  (1 , 3 , 2 , 4 );p Rint_r (Asort ( $arr 1 )?  $arr 1 :  "sort Failed" ); echo  ;p rint_r (asort ( $arr 2 )?  $arr 2 :  "sort Failed" );  

The result of the operation is:
Array ([0] = a [3] = b [2] = = c [1] = + D)
Array ([0] = 1 [2] = 2 [1] = 3 [3] = 4)

Arsort () function similarly

Ksort ()-Sorts the associative array in ascending order, based on the key

The ksort () function sorts the array by key name, preserving the original key for the array value.
Returns TRUE if successful, otherwise FALSE is returned.

<?php$arr1array(    ‘3‘‘第三个‘,    ‘2‘‘第二个‘,    ‘4‘‘第四个‘,    ‘1‘‘第一个‘,);print_r(ksort($arr1$arr1"排序失败");

The result of the operation is:
Array ([1] = = First [2] + = second [3] = + third [4] = fourth)

Krsort () function similarly



Supplemental-array_multisort () function

The Array_multisort () function returns a sorted array. You can enter one or more arrays. The function first sorts the first array, then the other arrays, and if two or more values are the same, it sorts the next array.

Note: The string key name is retained, but the numeric key name is re-indexed, starting at 0 and incrementing by 1.

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

<?php$a1=array("Dog","Dog","Cat");$a2=array(325);array_multisort($a1$a2, SORT_DESC);print_r($a1);print_r($a2);

The result of the operation is:
Array ([0] = Cat [1] = dog [2] = dog)
Array ([0] = 5 [1] = 3 [2] = 2)

PHP Array Sorting summary

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.