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

边栏推荐
- Cookie and session Basics
- 天才制造者:獨行俠、科技巨頭和AI|深度學習崛起十年
- 2. < tag dynamic programming and conventional problems > lt.343 integer partition
- Using Matplotlib to add an external image at the canvas level
- 一个从坟墓里爬出的公司
- Why do many Shopify independent station sellers use chat robots? Read industry secrets in one minute!
- Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
- C# 40. byte[]与16进制string互转
- File upload and security dog
- The first gift of the project, the flying oar contract!
猜你喜欢

Codeforces Round #802 (Div. 2)(A-D)

Dbeaver installation and configuration of offline driver

Image translation /gan:unsupervised image-to-image translation with self attention networks
![[unity3d] collider assembly](/img/de/29ecf4612c540e2df715f56c31cf1a.png)
[unity3d] collider assembly

Final review of brain and cognitive science

AD教程系列 | 4 - 创建集成库文件

zencart新建的URL怎么重写伪静态

【Unity3D】刚体组件Rigidbody

Classic theory: detailed explanation of three handshakes and four waves of TCP protocol

What is UWB in ultra-high precision positioning system
随机推荐
MySql如何删除所有多余的重复数据
Collections and dictionaries
C# 40. byte[]与16进制string互转
A ZABBIX self discovery script (shell Basics)
Keras actual combat cifar10 in tensorflow
Why do many Shopify independent station sellers use chat robots? Read industry secrets in one minute!
为什么许多shopify独立站卖家都在用聊天机器人?一分钟读懂行业秘密!
6.1 - 6.2 Introduction à la cryptographie à clé publique
Pytorch forecast house price
Astype conversion data type
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
ThreadPoolExecutor实现文件上传批量插入数据
【红队】要想加入红队,需要做好哪些准备?
程序人生
Zuul 實現動態路由
GD32F3x0 官方PWM驱动正频宽偏小(定时不准)的问题
Codeforces Round #800 (Div. 2)
Guanghetong and anti international bring 5g R16 powerful performance to the AI edge computing platform based on NVIDIA Jetson Xavier nx
一个从坟墓里爬出的公司
Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond