PHP array sorting

Source: Internet
Author: User
Tags sorts

PhpArray Sorting

The elements in the array can be sorted in descending or ascending order, in alphabetical or numerical terms.

PHP-Array sorting function

In this chapter, we will introduce the following PHP array sorting functions:

    • Sort ()-ascending order of arrays
    • Rsort ()-Descending order of the array
    • Asort ()-Sets the array in ascending order based on the values of the associative arrays
    • Ksort ()-an array is sorted in ascending order based on the keys of the associative arrays
    • Arsort ()-sorts arrays in descending order based on the values of the associative array
    • Krsort ()-Sorts the arrays in descending order based on the keys of the associative array
Sort ()-ascending order of arrays

The following instance arranges the elements in the $cars array in ascending alphabetical order:

Example <?php
$cars =array ("Volvo", "BMW", "Toyota");
Sort ($cars);
?>

The following instance arranges the elements in the $numbers array in ascending order of numbers:

Example <?php
$numbers =array (4,6,2,22,11);
Sort ($numbers);
?>

Rsort ()-Descending order of the array

The following instance arranges the elements in the $cars array in descending alphabetical order:

Example <?php
$cars =array ("Volvo", "BMW", "Toyota");
Rsort ($cars);
?>

The following instance arranges the elements in the $numbers array in descending order of numbers:

Example <?php
$numbers =array (4,6,2,22,11);
Rsort ($numbers);
?>

Asort ()-Sorts the array in ascending order based on the values of the arrays

The following instance sorts the associative array in ascending order based on the value of the array:

Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
Asort ($age);
?>

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

The following instance sorts the associative array in ascending order, based on the key of the array:

Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
Ksort ($age);
?>

Arsort ()-sorts arrays in descending order based on the values of the array

The following instance sorts the associative array in descending order, based on the value of the array:

Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
Arsort ($age);
?> Krsort ()-descending order of arrays based on the keys of the array

The following instance sorts the associative array in descending order, based on the key of the array:

Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
Krsort ($age);
?>

PHP array sorting

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.