Arr.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace App\Helper;
use Illuminate\Support\Collection;
/**
* 数组类函数
* Class Arrays
* @package App\Helper
* @author zbj
* @date 2023/4/15
*/
class Arr extends \Illuminate\Support\Arr
{
/**
* 把返回的数据集转换成Tree
* @param $list
* @param string $pk
* @param string $pid
* @param string $child
* @param int $root
* @return array
* @author zbj
* @date 2023/4/13
*/
public static function listToTree($list, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0)
{
// 创建Tree
$tree = array();
if (is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] = &$list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = &$list[$key];
} else {
if (isset($refer[$parentId])) {
$parent = &$refer[$parentId];
$parent[$child][] = &$list[$key];
}
}
}
}
return $tree;
}
/**
* 分隔字符串成数组并按照指定的函数过滤数组
* @param string $string
* @param string $filters
* @param string $delimiter
* @return array|false|string[]
* @author zbj
* @date 2023/4/13
*/
public static function splitFilterToArray($string, $filters = 'trim', $delimiter = ',')
{
if (!$string) {
return [];
}
$data = !is_array($string) ? explode($delimiter, $string) : $string;
$filters = explode(',', $filters);
if (!$filters) {//没有值或者没有过滤函数
return $data;
}
//过滤
foreach ($filters as $fun) {
if (!function_exists($fun)) {
continue;
}
$data = array_map($fun, $data);
}
return $data;
}
/**
* 只保留指定的key 最多支持二维数组
* @param array $rows 需要过滤的数组
* @param array $keepKeys 需要保留的键名
* @return array|array[]|mixed
* @author zbj
* @date 2023/4/15
*/
public static function twoKeepKeys(array $rows, array $keepKeys)
{
if (!$rows || !$keepKeys) {
return $rows;
}
//第一维有字符串键名 就过滤第一维
$signle = false;
$keys = array_keys($rows);
foreach ($keys as $k) {
if (!is_numeric($k)) {
$signle = true;
$rows = [$rows];
break;
}
}
//将数组将非法的键去掉
$rows = array_map(function ($item) use ($keepKeys) {
if (!is_array($item)) {
return $item;
}
$illegalKeys = array_diff(array_keys($item), $keepKeys);//获取非法键名
if ($illegalKeys) {
foreach ($illegalKeys as $key) {
unset($item[$key]);
}
}
return $item;
}, $rows);
return $signle ? $rows[0] : $rows;
}
/**
* 数组转字符串
* @param $arr
* @return string
* @author zbj
* @date 2023/4/17
*/
public static function a2s($arr): string
{
return json_encode($arr, JSON_UNESCAPED_UNICODE);
}
/**
* 字符串转数组
* @param $str
* @return array|mixed
* @author zbj
* @date 2023/4/17
*/
public static function s2a($str)
{
if(!$str){
return [];
}
if (is_array($str)) {
return $str;
}
return is_object($str) ? (array)$str : json_decode($str, true);
}
/**
* 数组转set形式字符串
* @param $arr
* @param string $format
* @return string
* @author zbj
* @date 2023/4/17
*/
public static function arrToSet($arr, string $format = 'intval'): string
{
$arr = array_unique(array_filter(Arr::splitFilterToArray($arr, $format, ',')));
return $arr ? implode(',', $arr) : '';
}
/**
* set形式字符串转数组
* @param $str
* @param string $format
* @return array
* @author zbj
* @date 2023/4/17
*/
public static function setToArr($str, string $format = 'intval')
{
if (is_string($str)) {
return Arr::splitFilterToArray($str, $format, ',');
}
return $str ?: [];
}
/**
* 将数组设置成某个键的值
* @param $arr
* @param $key
* @return array
* @author zbj
* @date 2023/5/16
*/
public static function setValueToKey($arr, $key)
{
$data = [];
if (!$arr) {
return $data;
}
foreach ($arr as $v) {
$data[$v[$key]] = $v;
}
if ($arr instanceof Collection) {
$data = new Collection($data);
}
return $data;
}
/**
* 根据列表查找指定id下的所有子id
* @param $dataArray
* @param $parentId
* @param $resultArray
* @author zbj
* @date 2023/10/17
*/
public static function findChildIds($dataArray, $parentId, &$resultArray) {
foreach ($dataArray as $value) {
if ($value['pid'] == $parentId) {
$resultArray[] = $value['id'];
self::findChildIds($dataArray, $value['id'], $resultArray);
}
}
}
}