当前位置:网站首页>php中通过集合collect的方法来实现把某个值插入到数组中指定的位置
php中通过集合collect的方法来实现把某个值插入到数组中指定的位置
2022-07-02 06:23:00 【夜空の雪風】
需求分析
最近在项目开发的过程中遇到列表中的排序要求按照能上下拖动的方式进行重新排序,例如:A,B,C,D,E。现在要把F插入到C后边,然后按照A,B,C,F,D,E进行排序。
解决方案
经过各种方法的尝试,发现通过集合collect中的一些方法可以快速的实现这个功能。
Thinkphp框架中和PHP原生项目可以直接通过composer安装。Laravel8中自带集合Illuminate\Support\Collection方法而可以直接进行使用。
//安装集合collect方法
composer require tightenco/collect
默认安装的为最新版本的安装包。
代码演示
/** * 实现在数组中的指定元素的前、后插入指定的值。 * @param [type] $sort 被插入的数组数据 * @param [type] $move_sort 要插入的元素 * @param [type] $choose_sort 被插入的元素 * @param [type] $type 1前方插入,2后方插入 * @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());
}
使用方法
$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)
结果演示
9排到了3的后边
{
"status": 200,
"message": "操作成功",
"data": [
1,
2,
3,
9,
4,
5,
6,
7,
8
]
}
方法分析
search 方法在集合中搜索给定的值并返回它的键。如果没有找到,则返回 false 。
filter 方法使用给定的回调函数过滤集合,只保留那些通过指定条件测试的集合项。
partition 是可以和 PHP 的 list 方法配合使用,利用回调返回是否为真来分开通过指定条件的元素以及那些不通过指定条件的元素。
附上集合文档。https://learnku.com/docs/laravel/8.x/collections/9390
边栏推荐
- JS delete the last bit of the string
- Review of reflection topics
- Latex warning: citation "*****" on page y undefined on input line*
- Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
- (the 100th blog) written at the end of the second year of doctor's degree -20200818
- Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
- 如何调试微信内置浏览器应用(企业号、公众号、订阅号)
- Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
- JS modification element attribute flipping commonly used in selenium's Web Automation
- js删除字符串的最后一个字符
猜你喜欢
![[literature reading and thought notes 13] unprocessing images for learned raw denoising](/img/a5/ed26a90b3edd75a37b2e5164f6b7d2.png)
[literature reading and thought notes 13] unprocessing images for learned raw denoising

搭建frp进行内网穿透

Sentry construction and use

The default Google browser cannot open the link (clicking the hyperlink does not respond)

Pytest (2) mark function

Sqli labs customs clearance summary-page3

js中map和forEach的用法

js中map和forEach的用法

sqli-labs通关汇总-page2

Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
随机推荐
unittest. Texttestrunner does not generate TXT test reports
Uploading attachments using Win32 in Web Automation
Stress test modification solution
2021-07-19C#CAD二次开发创建多线段
How to debug wechat built-in browser applications (enterprise number, official account, subscription number)
Sqli-labs customs clearance (less15-less17)
Latex compiles Chinese in vscode and solves the problem of using Chinese path
Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
Error "list" object is not callable in Web automatic switching window
Explanation and application of annotation and reflection
Eggjs -typeorm treeenity practice
ts和js区别
Go package name
Implement strstr() II
Log - 7 - record a major error in missing documents (A4 paper)
JS to determine whether there is a value in the object in the array
JS judge whether the object is empty
查询GPU时无进程运行,但是显存却被占用了
MySQL index
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved