当前位置:网站首页>PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
2022-06-26 05:10:00 【You are sexy!】
If you don't want to talk much, please go to the picture above :
Obviously this is a multidimensional array , According to the requirements of the front end , He asked me to $data['children'] Data inside , according to id To pass it on to him in ascending order .
Then this is simple .( You can't say you won't do it in front of the front end )
Let's first look at the data structure of the array
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["pid"]=>
int(0)
["cate_name"]=>
string(6) " menu "
["pic"]=>
string(0) ""
["children"]=>
array(6) {
[0]=>
array(3) {
["id"]=>
int(8)
["cate_name"]=>
string(9) " Local specialty "
["pic"]=>
string(0) ""
}
[1]=>
array(3) {
["id"]=>
int(7)
["cate_name"]=>
string(12) " Drinks "
["pic"]=>
string(0) ""
}
[2]=>
array(3) {
["id"]=>
int(6)
["cate_name"]=>
string(6) " Staple food "
["pic"]=>
string(0) ""
}
[3]=>
array(3) {
["id"]=>
int(5)
["cate_name"]=>
string(6) " Stir fry "
["pic"]=>
string(0) ""
}
[4]=>
array(3) {
["id"]=>
int(4)
["cate_name"]=>
string(6) " Cold Dishes "
["pic"]=>
string(0) ""
}
[5]=>
array(3) {
["id"]=>
int(2)
["cate_name"]=>
string(12) " Key features "
["pic"]=>
string(0) ""
}
}
}
}
Don't talk much , Look at how I put $data['children'] How the following data is in ascending order Sort of :
/************* This is the query . Can omit not to see ****************************/
// The first step is to query the data , I use it Tp6.0 frame
$cateogry = StoreCategory::with('children')
->where('is_show',1)
->order('sort desc,id asc')
->where('pid',0)
->hidden(['add_time','is_show','sort','children.sort','children.add_time','children.pid','children.is_show'])
->select()
->toArray();
/************* This is the query . Can omit not to see ****************************/
// The second step Take out the array to be arranged
$a = $cateogry[0]['children'];
// Step 3: take it out again Take out the key of the reference to be sorted
$cmf_arr = array_column($a, 'id');
// Fourth parts Sort in ascending order ( In this step, do not use variables to receive ,array_multisort() The return is a Boolean value )
array_multisort($cmf_arr,SORT_ASC, $a);
// Step five Reassign , Replace array
$cateogry[0]['children'] = $a;
// Step six , Back to the front end
return app('json')->success($cateogry);
Let's look at the results again :

Take another look at the data returned to the front end :

Okay , This has met the requirements of the front end API Asked for .

边栏推荐
- UWB超高精度定位系统原理图
- Practical cases | getting started and mastering tkinter+pyinstaller
- A company crawling out of its grave
- What is UWB in ultra-high precision positioning system
- [ide (imagebed)]picgo+typora+aliyunoss deployment blog Gallery (2022.6)
- ModuleNotFoundError: No module named ‘numpy‘
- Some parameter settings and feature graph visualization of yolov5-6.0
- ECCV 2020 double champion team, take you to conquer target detection on the 7th
- Zuul 实现动态路由
- Computer Vision Tools Chain
猜你喜欢

广和通联合安提国际为基于英伟达 Jetson Xavier NX的AI边缘计算平台带来5G R16强大性能

Status of processes and communication between processes

关于支付接口回调地址参数字段是“notify_url”,签名过后的特殊字符url编码以后再解码后出现错误(¬ , ¢, ¤, £)

Illustration of ONEFLOW's learning rate adjustment strategy

pycharm 导包错误没有警告
Why does the mobile IM based on TCP still need to keep the heartbeat alive?

UWB超高精度定位系统架构图

【上采样方式-OpenCV插值】

Zuul 實現動態路由

天才制造者:独行侠、科技巨头和AI|深度学习崛起十年
随机推荐
Wechat applet exits the applet (navigator and api--wx.exitminiprogram)
-Discrete Mathematics - Analysis of final exercises
Beidou navigation technology and industrial application of "chasing dreams in space and feeling for Beidou"
YOLOv5-6.0的一些参数设置和特征图可视化
What is UWB in ultra-high precision positioning system
Procedural life
出色的学习能力,才是你唯一可持续的竞争优势
Codeforces Round #800 (Div. 2)
【Unity3D】刚体组件Rigidbody
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
Vie procédurale
ThreadPoolExecutor implements file uploading and batch inserting data
Selection of programming language
PowerShell runtime system IO exceptions
Zuul implements dynamic routing
Sentimentin tensorflow_ analysis_ layer
Zuul 实现动态路由
红队得分方法统计
程序人生
微服务之间的Token传递之一@Feign的token传递