当前位置:网站首页>对PHP数组进行自定义排序
对PHP数组进行自定义排序
2022-08-07 22:53:00 【reg183】
usort — 使用用户自定义的比较函数对数组中的值进行排序
Note: 此函数将为array填充新的键名,将会删除原有键名
function asc($a,$b){
return $a>$b?1:-1;
}
$b=array('2','6','8','9','1','7');
usort($b,'asc');
print_r($b);
//输出 Array ( [0] => 1 [1] => 2 [2] => 6 [3] => 7 [4] => 8 [5] => 9 )
判断条件为 return $a>$b?1:-1; 数组值按照从小到大排序
function re($a,$b){
$aa = array(7,5,3,1,2,4,9,8,6,9);
foreach($aa as $k => $v){
if($a==$v){
$ai = $k;
}
if($b==$v){
$bi = $k;
}
}
return ($ai>$bi)?1:-1;
}
$x=array(
array('1','2','3','4','5','6'),
array('2','6','8','9','1','8'),
array('3','5','4','7','6','8'),
array('4','5','8','1','9','6'),
array('5','8','1','2','4','6'),
);
foreach($x as &$v){
usort($v,'re');
}
print_r($x);
Array ( [0] => Array ( [0] => 5 [1] => 3 [2] => 1 [3] => 2 [4] => 4 [5] => 6 )
[1] => Array ( [0] => 1 [1] => 2 [2] => 8 [3] => 8 [4] => 6 [5] => 9 )
[2] => Array ( [0] => 7 [1] => 5 [2] => 3 [3] => 4 [4] => 8 [5] => 6 )
[3] => Array ( [0] => 5 [1] => 1 [2] => 4 [3] => 8 [4] => 6 [5] => 9 )
[4] => Array ( [0] => 5 [1] => 1 [2] => 2 [3] => 4 [4] => 8 [5] => 6 ) )
这时候数组按照给定的顺序从左往右排序
边栏推荐
- 买股票用通达信安全吗?资金会不会被转走?
- Application of Matlab in 3D Vision 01 Display PCD point cloud
- Mongodb出现Error: couldn‘t add user: Could not find role: [email protected] 解决方法
- UE4 Sequence adds basic animation effects (01-object movement)
- 2022年危险化学品生产单位安全生产管理人员考试试题及模拟考试
- Fiftyone的安装和使用
- 论文翻译:2021_LACOPE: Latency-Constrained Pitch Estimation for Speech Enhancement
- The generation process of Presto Stage
- PersFormer: 3D Lane Detection via Perspective Transformer and the OpenLane Benchmark 论文笔记
- 解决执行Command报错exit status 255
猜你喜欢

时间复杂度

2022年危险化学品生产单位安全生产管理人员考试试题及模拟考试

力扣206,反转链表

【学生个人网页设计作品】使用HMTL制作一个超好看的保护海豚动物网页

论文翻译:2021_LACOPE: Latency-Constrained Pitch Estimation for Speech Enhancement

UE4 Sequence添加基础动画效果 (02-切换动作)

云服务器中mongodb配置账号密码 图文讲解(全)

如何找到系统支持的最大并发数
![[Students' personal web design works] Use HMTL to make a super beautiful web page for protecting dolphins](/img/7a/a8ec8c07b4357128709a3215b2e07f.png)
[Students' personal web design works] Use HMTL to make a super beautiful web page for protecting dolphins

第一次小实习记录
随机推荐
2022G1工业锅炉司炉特种作业证考试题库模拟考试平台操作
【UiPath2022+C#】UiPath 调试
Hands-on deep learning_target detection
Mongodb出现Error: couldn‘t add user: Could not find role: [email protected] 解决方法
【无标题】
银行科技岗面试
SQL DNSlog注入详解
Redis分布式锁
SQL堆叠注入详解
Unity editor extension -- Hierarchy extension
The Unity editor development, Project development
Shell: Loop Statements and Functions
无代码平台日期入门教程
论文翻译:2021_LACOPE: Latency-Constrained Pitch Estimation for Speech Enhancement
my JDBC
【云原生--Kubernetes】PV、PVC
awk 统计日志中的ip和userId--分析用户恶意刷接口行为
flink
图数据建模从关系型数据到图数
Project ‘cv_bridge‘ specifies ‘/usr/include/opencv‘ as an include dir, which is not found的解决方法