<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); arsort($fruits);//按照字符逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } echo "<p>"; asort($fruits);//按照字符正向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 b = orange a = lemon c = banana d = apple d = apple c = banana a = lemon b = orange */
krsort
功能:按照键名进行逆向排序
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); krsort($fruits);//按照键名逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 d = apple c = banana b = orange a = lemon */
ksort
功能:按照键名进行正向排序
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); ksort($fruits);//按照键名正向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 a = lemon b = orange c = banana d = apple */
rsort
功能:按照值进行逆向排序,键名改变
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); rsort($fruits);//按照值进行逆向排序或数字,键名改变 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 0 = orange 1 = lemon 2 = banana 3 = apple */
sort
功能:按照值进行正向排序,键名改变
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); sort($fruits);//按照值进行逆向排序或数字,键名改变 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 0 = apple 1 = banana 2 = lemon 3 = orange */
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php常用函数与技巧总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》