Files
freeCodeCamp/guide/chinese/php/arrays/sorting-arrays/index.md
RichardLimSpring e889e4e5d1 Edit on few texts (#29621)
* Edit on few texts

The first sort() function title was initially in Chinese, translated to the actual name of the function. 
The asort() function title was initially in uppercase, might confuse reader whether uppercase or lowercase to be user, converted to lowercase.

* Add New Book

Added Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning PHP, MYSQL, Javascript, CSS & HTML5) to the list.
2018-11-27 13:12:20 -08:00

3.0 KiB
Raw Blame History

title, localeTitle
title localeTitle
Sorting Arrays 排序数组

排序数组

PHP提供了几种排序数组的函数。本页介绍了不同的功能并包含了示例。

sort

sort()函数按升序字母/数字顺序对数组的值进行排序例如ABCDE ... 5,4,3,2,1 ......

<?php 
 $freecodecamp = array("free", "code", "camp"); 
 sort($freecodecamp); 
 print_r($freecodecamp); 

输出:

Array 
 ( 
    [0] => camp 
    [1] => code 
    [2] => free 
 ) 

rsort

rsort()函数按降序字母/数字顺序对数组的值进行排序例如ZYXWV ... rsort() ......

<?php 
 $freecodecamp = array("free", "code", "camp"); 
 rsort($freecodecamp); 
 print_r($freecodecamp); 

输出:

Array 
 ( 
    [0] => free 
    [1] => code 
    [2] => camp 
 ) 

asort

asort()函数按字母顺序/数字顺序例如ABCDE ... 5,4,3,2,1 ......)对其关联数组进行排序。

<?php 
 $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); 
 asort($freecodecamp); 
 print_r($freecodecamp); 

输出:

Array 
 ( 
    [two] => camp 
    [one] => code 
    [zero] => free 
 ) 

ksort

ksort()函数按字母/数字顺序按字母顺序排列关联数组例如ABCDE ... 5,4,3,2,1 ......

<?php 
 $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); 
 ksort($freecodecamp); 
 print_r($freecodecamp); 

输出:

Array 
 ( 
    [one] => code 
    [two] => camp 
    [zero] => free 
 ) 

arsort

arsort()函数按字母/数字顺序按字母顺序排列一个关联数组例如ZYXWV ... 5,4,3,2,1 ......

<?php 
 $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); 
 arsort($freecodecamp); 
 print_r($freecodecamp); 

输出:

Array 
 ( 
    [zero] => free 
    [one] => code 
    [two] => camp 
 ) 

krsort

krsort()函数按字母/数字顺序降序排序关联数组例如ZYXWV ... krsort() ......

<?php 
 $freecodecamp = array("zero"=>"free", "one"=>"code", "two"=>"camp"); 
 krsort($freecodecamp); 
 print_r($freecodecamp); 

输出:

Array 
 ( 
    [zero] => free 
    [two] => camp 
    [one] => code 
 ) 

更多信息: