The technique of inductive _php of common PHP array sort function

Source: Internet
Author: User
Tags array sort numeric sorts

Array sorting: Elements in an array can be sorted in ascending or descending order in alphabetical or numeric sequence.
commonly used PHP array sorting function
sort ()-sort the array in ascending order
Rsort ()-Sort in descending pairs
Asort ()-Sort the associative array in ascending order by value
Ksort ()-Sort the associative array in ascending order according to the key
Arsort ()-Sort the associative array in descending order by value
Krsort ()-Sort the associative array in descending order according to the key

Example:

Sort ()-sort the array in ascending order

Note: This function assigns a new key name to the cell in the array. The original key name was deleted.
Return true Failure returns false if successful
For example:

<?php
$arr 1 = Array (' A ', ' d ', ' C ', ' B ');
$arr 2 = Array (1, 3, 2, 4);
Print_r ($arr 1) $arr 1: "Sort Failed");
echo "<br>";
Print_r ($arr 2) $arr 2: "Sort failed");

The results of the operation are:
Array ([0] => a [1] => b [2] => C [3] => D)
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

The Rsort () function is equally

Asort ()-Sort the associative array in ascending order by value

The Asort () function sorts the array and maintains an indexed relationship. It is primarily used to sort the associative arrays that are important to the order of the cells.
Returns TRUE if successful, otherwise returns FALSE.

<?php
$arr 1 = Array (' A ', ' d ', ' C ', ' B ');
$arr 2 = Array (1, 3, 2, 4);
Print_r (Asort ($arr 1)? $arr 1: "Sort Failed");
echo "<br>";
Print_r (Asort ($arr 2)? $arr 2: "Sort failed");

The results of the operation are:
Array ([0] => a [3] => b [2] => C [1] => D)
Array ([0] => 1 [2] => 2 [1] => 3 [3] => 4)

The Arsort () function is equally

Ksort ()-Sort the associative array in ascending order according to the key

The Ksort () function sorts by an array of key names, preserving the original key for the array value.
If successful, returns TRUE, otherwise returns FALSE.

<?php
$arr 1 = Array (
  ' 3 ' => ' third ', '
  2 ' => ' second ',
  ' 4 ' => ' fourth ',
  ' 1 ' => ' first ', 
   );
Print_r (Ksort ($arr 1)? $arr 1: "Sort failed");

The results of the operation are:
Array ([1] => first [2] => second [3] => third [4] => Fourth)

The Krsort () function is equally

Supplemental-array_multisort () function

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

Note: The string key name is preserved, but the numeric key name is indexed, starting at 0 and incremented 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
$a 1=array ("Dog", "Dog", "Cat");
$a 2=array (3, 2, 5);
Array_multisort ($a 1, SORT_ASC, $a 2, sort_desc);
Print_r ($a 1);
Print_r ($a 2);

The results of the operation are:
Array ([0] => Cat [1] => Dog [2] => Dog)
Array ([0] => 5 [1] => 3 [2] => 2)

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.