PHP Array Sorting

Source: Internet
Author: User
Tags sorts

PHP-sorting functions for arrays
In this section, we will learn the following 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
Sorting an array in ascending order-sort ()
The following examples of Kim are sorted alphabetically by the elements in the array $cars:
Instance
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Sort ($cars);
?>
Running an instance
The following example sorts the elements in an array $numbers in ascending numbers:
Instance
<?php
$numbers =array (3,5,1,22,11);
Sort ($numbers);
?>
Running an instance
Sorting arrays in descending order-rsort ()
The following example sorts the elements in the array $cars in descending alphabetical order:
Instance
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Rsort ($cars);
?>
Running an instance
The following example sorts the elements in an array $numbers in descending order of numbers:
Instance
<?php
$numbers =array (3,5,1,22,11);
Rsort ($numbers);
?>
Running an instance
Sort ascending by value of an array-asort ()
The following example sorts the associative arrays in ascending order based on values:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Asort.www.bjrongjinhuiyin.com ($age);
?>
Running an instance
Sort ascending by Key array-ksort ()
The following example sorts an associative array in ascending order based on the key:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Ksort ($age);
?>
Running an instance
Sort Descending by Value array-arsort ()
The following example sorts the associative arrays by value in descending order:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Arsort ($age);
?>
Running an instance
Sort descending order of keys by Key-Krsort ()
The following example sorts the associative arrays in descending order based on the key:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" 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.