当前位置:网站首页>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 .

边栏推荐
- 一个从坟墓里爬出的公司
- [unity3d] collider assembly
- 86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
- data = self._data_queue.get(timeout=timeout)
- Zuul 實現動態路由
- Why does the mobile IM based on TCP still need to keep the heartbeat alive?
- Datetime data type - min() get the earliest date and date_ Range() creates a date range, timestamp() creates a timestamp, and tz() changes the time zone
- 天才制造者:独行侠、科技巨头和AI|深度学习崛起十年
- 【Latex】错误类型总结(持更)
- Mise en œuvre du routage dynamique par zuul
猜你喜欢

Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages

图解OneFlow的学习率调整策略

Zuul implements dynamic routing

一个从坟墓里爬出的公司

DBeaver 安装及配置离线驱动

The first gift of the project, the flying oar contract!

6.1 - 6.2 公鑰密碼學簡介

Use fill and fill in Matplotlib_ Between fill the blank area between functions

《财富自由之路》读书之一点体会

为什么许多shopify独立站卖家都在用聊天机器人?一分钟读懂行业秘密!
随机推荐
The best Chinese open source class of vision transformer, ten hours of on-site coding to play with the popular model of Vit!
[greedy college] Figure neural network advanced training camp
date_ Range creation date range freq parameter value table and creation example
Excellent learning ability is your only sustainable competitive advantage
0622 horse palm fell 9%
Computer Vision Tools Chain
Tp5.0框架 PDO连接mysql 报错:Too many connections 解决方法
Difference between return and yield
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
Illustration of ONEFLOW's learning rate adjustment strategy
Create a binary response variable using the cut sub box operation
[latex] error type summary (hold the change)
Anaconda creates tensorflow environment
百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!
Solution to back-off restarting failed container
Dbeaver installation and configuration of offline driver
超高精度定位系统中的UWB是什么
Some parameter settings and feature graph visualization of yolov5-6.0
Zuul 實現動態路由
【Unity3D】人机交互Input