1 | <?php |
||
2 | |||
3 | // +---------------------------------------------------------------------- |
||
4 | // | ThinkLibrary 6.0 for ThinkPhP 6.0 |
||
5 | // +---------------------------------------------------------------------- |
||
6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||
7 | // +---------------------------------------------------------------------- |
||
8 | // | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
||
9 | // +---------------------------------------------------------------------- |
||
10 | // | 开源协议 ( https://mit-license.org ) |
||
11 | // +---------------------------------------------------------------------- |
||
12 | // | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
||
13 | // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
||
14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||
15 | // +---------------------------------------------------------------------- |
||
16 | |||
17 | declare (strict_types=1); |
||
18 | |||
19 | namespace DtApp\ThinkLibrary\extend; |
||
20 | |||
21 | /** |
||
22 | * 数组扩展 |
||
23 | * Class ArraysExtend |
||
24 | * @package DtApp\ThinkLibrary\extend |
||
25 | */ |
||
26 | class ArraysExtend |
||
27 | { |
||
28 | /** |
||
29 | * 数组随机返回一个下标 |
||
30 | * @param array $array |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public static function rand(array $array) |
||
34 | { |
||
35 | return array_rand($array); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * 数组随机返回一个值 |
||
40 | * @param array $array |
||
41 | * @return mixed |
||
42 | */ |
||
43 | public static function randValue(array $array) |
||
44 | { |
||
45 | return $array[array_rand($array)]; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * 分隔数组 |
||
50 | * @param array $array 数组 |
||
51 | * @param int $num 数量 |
||
52 | * @return array |
||
53 | */ |
||
54 | public static function split(array $array, $num = 5): array |
||
55 | { |
||
56 | $arrRet = array(); |
||
57 | if (!isset($array) || empty($array)) { |
||
58 | return $arrRet; |
||
59 | } |
||
60 | $iCount = count($array) / $num; |
||
61 | if (!is_int($iCount)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
62 | $iCount = ceil($iCount); |
||
63 | } else { |
||
64 | ++$iCount; |
||
65 | } |
||
66 | for ($i = 0; $i < $iCount; ++$i) { |
||
67 | $arrInfos = array_slice($array, $i * $num, $num); |
||
68 | if (empty($arrInfos)) { |
||
69 | continue; |
||
70 | } |
||
71 | $arrRet[] = $arrInfos; |
||
72 | unset($arrInfos); |
||
73 | } |
||
74 | return $arrRet; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * 多维数组去重 |
||
79 | * @param array $array |
||
80 | * @return array |
||
81 | */ |
||
82 | public static function unique(array $array): array |
||
83 | { |
||
84 | $out = array(); |
||
85 | foreach ($array as $key => $value) { |
||
86 | if (!in_array($value, $out, true)) { |
||
87 | $out[$key] = $value; |
||
88 | } |
||
89 | } |
||
90 | $out = array_values($out); |
||
91 | return $out; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * 二维数组根据某个键排序 |
||
96 | * @param array $arrays |
||
97 | * @param string $sort_key |
||
98 | * @param int $sort_order |
||
99 | * @param int $sort_type |
||
100 | * @return array |
||
101 | */ |
||
102 | public static function sort(array $arrays, string $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC): array |
||
103 | { |
||
104 | $key_arrays = array(); |
||
105 | if (is_array($arrays)) { |
||
0 ignored issues
–
show
|
|||
106 | foreach ($arrays as $array) { |
||
107 | if (is_array($array)) { |
||
108 | $key_arrays[] = $array[$sort_key]; |
||
109 | } else { |
||
110 | return []; |
||
111 | } |
||
112 | } |
||
113 | } else { |
||
114 | return []; |
||
115 | } |
||
116 | array_multisort($key_arrays, $sort_order, $sort_type, $arrays); |
||
117 | return $arrays; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * 数组删除空格 |
||
122 | * @param array $arr |
||
123 | * @return array |
||
124 | */ |
||
125 | public static function trimArray(array $arr) |
||
126 | { |
||
127 | if (!is_array($arr)) { |
||
0 ignored issues
–
show
|
|||
128 | return $arr; |
||
129 | } |
||
130 | foreach ($arr as $key => $value) { |
||
131 | if (is_array($value)) { |
||
132 | $arr[$key] = self::TrimArray($value); |
||
133 | } else { |
||
134 | $arr[$key] = self::trimAll($value); |
||
135 | } |
||
136 | } |
||
137 | return $arr; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * 字符串删除空格 |
||
142 | * @param $str |
||
143 | * @return string|string[] |
||
144 | */ |
||
145 | private static function trimAll($str) |
||
146 | { |
||
147 | $oldchar = array(" ", " ", "\t", "\n", "\r"); |
||
148 | $newchar = array("", "", "", "", ""); |
||
149 | return str_replace($oldchar, $newchar, $str); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * 把json字符串或json对象转json数组 |
||
154 | * @param $output |
||
155 | * @return array |
||
156 | */ |
||
157 | public static function toArray($output): array |
||
158 | { |
||
159 | if (is_array($output)) { |
||
160 | return $output; |
||
161 | } |
||
162 | if (is_object($output)) { |
||
163 | $output = json_encode($output, JSON_UNESCAPED_UNICODE); |
||
164 | } |
||
165 | return json_decode($output, true); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param $array |
||
170 | * @param $name |
||
171 | * @return array |
||
172 | */ |
||
173 | public static function valChunk($array, $name): array |
||
174 | { |
||
175 | $result = array(); |
||
176 | $ar2 = []; |
||
177 | foreach ($array as $key => $value) { |
||
178 | foreach ($array as $k => $val) { |
||
179 | if ($value[(string)($name)] == $val[(string)($name)]) { |
||
180 | $ar2[] = $val; |
||
181 | } |
||
182 | } |
||
183 | $result[$value[(string)($name)]] = $ar2; |
||
184 | $ar2 = []; |
||
185 | } |
||
186 | return $result; |
||
187 | } |
||
188 | } |
||
189 |