当前位置:网站首页>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
边栏推荐
- Oracle段顾问、怎么处理行链接行迁移、降低高水位
- 图解Kubernetes中的etcd的访问
- 在php的开发环境中如何调取WebService?
- SSM学生成绩信息管理系统
- 離線數倉和bi開發的實踐和思考
- MySQL组合索引加不加ID
- Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
- MySQL中的正则表达式
- php中的二维数组去重
- 【信息检索导论】第三章 容错式检索
猜你喜欢

sqli-labs通關匯總-page2

Sqli labs customs clearance summary-page1

第一个快应用(quickapp)demo

Ding Dong, here comes the redis om object mapping framework

【论文介绍】R-Drop: Regularized Dropout for Neural Networks

ssm垃圾分类管理系统

Principle analysis of spark

Build FRP for intranet penetration

Sqli labs customs clearance summary-page2

如何高效开发一款微信小程序
随机推荐
oracle EBS标准表的后缀解释说明
CRP实施方法论
IDEA2020中PySpark的两表关联(字段名相同)
UEditor . Net version arbitrary file upload vulnerability recurrence
Three principles of architecture design
ORACLE APEX 21.2安裝及一鍵部署
php中判断版本号是否连续
DNS攻击详解
php中的二维数组去重
Oracle apex Ajax process + dy verification
Yolov5 practice: teach object detection by hand
Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
Explain in detail the process of realizing Chinese text classification by CNN
Thinkphp5中一个字段对应多个模糊查询
Alpha Beta Pruning in Adversarial Search
Build FRP for intranet penetration
Check log4j problems using stain analysis
MySQL无order by的排序规则因素
2021-07-05c /cad secondary development create arc (4)
Oracle 11g uses ords+pljson to implement JSON_ Table effect