当前位置:网站首页>PHP uses the method of collecting to insert a value into the specified position in the array
PHP uses the method of collecting to insert a value into the specified position in the array
2022-07-02 07:19:00 【Snow wind in the night sky】
Demand analysis
Recently, in the process of project development, the sorting in the list requires reordering by dragging up and down , for example :A,B,C,D,E. Now we have to put F Insert into C Back , And then according to A,B,C,F,D,E Sort .
Solution
After trying various methods , Discover through set collect Some methods in can realize this function quickly .
Thinkphp Frame neutralization PHP Native projects can be directly through composer install .Laravel8 Set in Illuminate\Support\Collection Method can be used directly .
// Installation collection collect Method
composer require tightenco/collect
The latest version of the installation package is installed by default .
Code demonstration
/** * Implement the first of the specified elements in the array 、 Insert the specified value after . * @param [type] $sort Inserted array data * @param [type] $move_sort Elements to insert * @param [type] $choose_sort The inserted element * @param [type] $type 1 Insert in front ,2 Insert after * @return void */
function sort_edit($sort, $move_sort, $choose_sort, $type)
{
$sortCollection = collect($sort);
$index = $sortCollection->search($choose_sort);
[$left, $right] = $sortCollection->filter(function ($item) use ($move_sort) {
return $item != $move_sort;
})
->partition(function ($item, $key) use ($index, $type) {
if ($type == 1) {
return $key < $index;
} else {
return $key <= $index;
}
});
$left->push($move_sort);
return $left->values()->merge($right->values());
}
Usage method
$sort = [1, 2, 3, 4, 5, 6, 7, 8];
$move_sort = 9;
$choose_sort = 3;
$type = 2;
sort_edit($sort, $move_sort, $choose_sort, $type)
Result demonstration
9 It's in line 3 Behind
{
"status": 200,
"message": " Successful operation ",
"data": [
1,
2,
3,
9,
4,
5,
6,
7,
8
]
}
Methods to analyze
search Method searches the collection for a given value and returns its key . If not found , Then return to false .
filter Method uses the given callback function to filter the collection , Only those set items that pass the specified condition test .
partition Is that you can and PHP Of list Method in combination with , Whether the callback return is true is used to separate the elements that pass the specified condition and those that do not pass the specified condition .
Attach collection document .https://learnku.com/docs/laravel/8.x/collections/9390
边栏推荐
- RMAN incremental recovery example (1) - without unbacked archive logs
- 【信息检索导论】第一章 布尔检索
- ARP攻击
- Proteus -- RS-232 dual computer communication
- Laravel8中的find_in_set、upsert的使用方法
- Use of interrupt()
- 一个中年程序员学习中国近代史的小结
- RMAN增量恢复示例(1)-不带未备份的归档日志
- Analysis of MapReduce and yarn principles
- 【模型蒸馏】TinyBERT: Distilling BERT for Natural Language Understanding
猜你喜欢

ssm垃圾分类管理系统

Error in running test pyspark in idea2020

如何高效开发一款微信小程序

The first quickapp demo

ssm人事管理系统

Check log4j problems using stain analysis

Changes in foreign currency bookkeeping and revaluation general ledger balance table (Part 2)

架构设计三原则

Network security -- intrusion detection of emergency response

Oracle apex Ajax process + dy verification
随机推荐
Spark SQL task performance optimization (basic)
优化方法:常用数学符号的含义
Oracle段顾问、怎么处理行链接行迁移、降低高水位
sqli-labs通關匯總-page2
ARP攻击
MySQL无order by的排序规则因素
ERNIE1.0 与 ERNIE2.0 论文解读
parser.parse_args 布尔值类型将False解析为True
【调参Tricks】WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach
使用 Compose 实现可见 ScrollBar
ARP attack
矩阵的Jordan分解实例
Go common compilation fails
类加载器及双亲委派机制
ORACLE EBS DATAGUARD 搭建
使用Matlab实现:Jacobi、Gauss-Seidel迭代
DNS攻击详解
SSM学生成绩信息管理系统
Oracle EBs and apex integrated login and principle analysis
Agile development of software development pattern (scrum)