数组排序,PHP
后端开发-php教程
PHP中数组是较为常用的数据类型,如何对数组中的数据进行排序,以方便对数据进行管理,本文就带大家一起来看一看如何利用PHP内置函数对数组进行排序。闲来麻将棋牌源码,vscode自带debug,ubuntu 命令行关机,tomcat口碑,sqlite通用仓储,一个服务器能放多少网站,ecshop退换货插件v2版,腾讯前端框架happ,汽车之家 反爬虫,电梯php,重庆鸿伟seo,网站版权界面还是源码,网页表情包插件,单图放大模板,首页页面图片轮播图,星外主机管理系统,java简单的绘图程序lzw
1.依据数组键值对数组排序国土资源局网站源码,ubuntu17 ssh,tomcat大服务器,蚯蚓是爬虫,php化疗方案啥意思,seo质量lzw
<?php$arr1 = array(3,1,5,2,0);sort($arr1);print_r($arr1);echo "
";$arr2 = array(3,1,5,2,0);rsort($arr2);print_r($arr2);?>
输出:Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 5 ) Array ( [0] => 5 [1] => 3 [2] => 2 [3] => 1 [4] => 0 )
sort()
– 对数组进行升序排列; rsort()
– 对数组进行降序排列
app游戏源码搭建教程,ubuntu如何有限连接,java爬虫容易吗,php vlan,李沧区seo优化lzw
2.依据关联数组键值对数组排序"lemon", "a" => "orange", "b" => "banana", "c" => "apple");arsort($fruits1);foreach ($fruits1 as $key => $val) { echo "$key = $val;";}echo "
";$fruits2 = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");asort($fruits2);foreach ($fruits2 as $key => $val) { echo "$key = $val\n";}?>
输出:a = orange;d = lemon;b = banana;c = apple; c = apple b = banana d = lemon a = orange
arsort()
– 根据关联数组的值,对数组进行降序排列;asort()
– 根据关联数组的值,对数组进行升序排列
3. 根据关联数组的键对数组排序
"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");krsort($fruits1);foreach ($fruits1 as $key => $val) { echo "$key = $val\n";}echo "
";$fruits2 = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");ksort($fruits2);foreach ($fruits2 as $key => $val) { echo "$key = $val\n";}?>
输出:d = lemon c = apple b = banana a = orange a = orange b = banana c = apple d = lemon
krsort()
– 根据关联数组的键,对数组进行降序排列;ksort()
– 根据关联数组的键,对数组进行升序排列
推荐:《2021年PHP面试题大汇总(收藏)》《php视频教学》