php 数组从小到大排序,PHP 数组排序-php数组的排序函数
PHP - 数组的排序函数
在本节中PHP数组排序,我们将学习如下 PHP 数组排序函数:
sort() - 以升序对数组排序
rsort() - 以降序对数
数组中的元素能够以字母或数字顺序进行升序或降序排序。 PHP - 数组的排序函数 在本节中PHP数组排序,我们将学习如下 PHP 数组排序函数: sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组进行排序 ksort() - 根据键,以升序对关联数组进行排序 arsort() - 根据值,以降序对关联数组进行排序 krsort() - 根据键,以降序对关联数组进行排序 对数组进行升序排序 - sort() 下面的例子按照字母升序对数组 $cars 中的元素进行排序: 实例 $cars=array("porsche","BMW","Volvo"); sort($cars); 运行: $cars=array("porsche","BMW","Volvo"); sort($cars); $clength=count($cars); for($x=0;$x { echo $cars[$x]; echo " "; } 运行结果:BMW Volvo porsche 下面的例子按照数字升序对数组 $numbers 中的元素进行排序: 实例 $numbers=array(3,5,1,22,11); sort($numbers); $arrlength=count($numbers); for($x=0;$x { echo $numbers[$x]; echo " "; } 运行结果:1 3 5 11 22 对数组进行降序排序 - rsort() 下面的例子按照字母降序对数组 $cars 中的元素进行排序: 实例 $cars=array("porsche","BMW","Volvo"); rsort($cars); $clength=count($cars); for($x=0;$x { echo $cars[$x]; echo " "; } 运行结果:porsche Volvo BMW 下面的例子按照数字降序对数组 $numbers 中的元素进行排序: $numbers=array(3,5,1,22,11); rsort($numbers); $arrlength=count($numbers); for($x=0;$x { echo $numbers[$x]; echo " "; } 根据值对数组进行升序排序 - asort() 下面的例子根据值对关联数组进行升序排序: $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); asort($age); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo " "; } 运行结果:Key=Elon, Value=47 Key=Steve, Value=56 Key=Bill, Value=63 根据键对数组进行升序排序 - ksort() 下面的例子根据键对关联数组进行升序排序: 实例 $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); ksort($age); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo " "; } 运行结果Key=Bill, Value=63 Key=Elon, Value=47 Key=Steve, Value=56 根据值对数组进行降序排序 - arsort() 下面的例子根据值对关联数组进行降序排序: $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); arsort($age); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo " "; } 运行结果:Key=Bill, Value=63 Key=Steve, Value=56 Key=Elon, Value=47 根据键对数组进行降序排序 - krsort() 下面的例子根据键对关联数组进行降序排序: 实例 $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); krsort($age); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo " "; } 运行结果Key=Steve, Value=56 Key=Elon, Value=47 Key=Bill, Value=63 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |