<?php
echo "<h2>数组的排序函数</h2>";
/*
•sort() - 以升序对数组排序
•rsort() - 以降序对数组排序
•asort() - 根据值,以升序对关联数组进行排序
•ksort() - 根据键,以升序对关联数组进行排序
•arsort() - 根据值,以降序对关联数组进行排序
•krsort() - 根据键,以降序对关联数组进行排序
*/
$cars=array("Volor","BMW","SAAB");
echo "<pre>";
print_r($cars);
echo "</pre>";
sort($cars);
echo "<pre>";
print_r($cars);
echo "</pre>";
/*输出
Array
(
[0] => Volor
[1] => BMW
[2] => SAAB
)
Array
(
[0] => BMW
[1] => SAAB
[2] => Volor
)
*/
//根据键对数组进行升序排序 - ksort()
$val=array('2'=>"hui",'1'=>"dao",'45'=>"ert");
echo "<pre>";
print_r($val);
echo "</pre>";
ksort($val);
echo "<pre>";
print_r($val);
echo "</pre>";
/*
Array
(
[2] => hui
[1] => dao
[45] => ert
)
Array
(
[1] => dao
[2] => hui
[45] => ert
)
*/
?>
16 PHP 数组排序函数
最新推荐文章于 2022-10-13 01:02:13 发布