Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
collection.php
1<?php
3
5
7{
24 public static function sortByColumn(array &$array, $columns, $callbacks = '', $defaultValueIfNotSetValue = null, $preserveKeys = false)
25 {
26 //by default: sort by ASC
27 if (!is_array($columns))
28 {
29 $columns = array($columns => SORT_ASC);
30 }
31 $params = $preserveDataKeys = array();
32 $alreadyFillPreserveDataKeys = false;
33 foreach ($columns as $column => &$order)
34 {
35 $callback = null;
36 //this is an array of callbacks (callable string)
37 if(is_array($callbacks) && !is_callable($callbacks))
38 {
39 //if callback set for column
40 if(!empty($callbacks[$column]))
41 {
42 $callback = is_callable($callbacks[$column])? $callbacks[$column] : false;
43 }
44 }
45 //common callback
46 elseif(!empty($callbacks))
47 {
48 $callback = is_callable($callbacks)? $callbacks : false;
49 }
50
51 if($callback === false)
52 {
53 throw new \Bitrix\Main\ArgumentOutOfRangeException('callbacks');
54 }
55
56 //this is similar to the index|slice
57 $valueColumn[$column] = array();
58 foreach ($array as $index => $row)
59 {
60 $value = $row[$column] ?? $defaultValueIfNotSetValue;
61 if ($callback)
62 {
63 $value = call_user_func_array($callback, array($value));
64 }
65 $valueColumn[$column][$index] = $value;
66 if($preserveKeys && !$alreadyFillPreserveDataKeys)
67 {
68 $preserveDataKeys[$index] = $index;
69 }
70 }
71 unset($row, $index);
72 $alreadyFillPreserveDataKeys = $preserveKeys && !empty($preserveDataKeys);
73 //bug in 5.3 call_user_func_array
74 $params[] = &$valueColumn[$column];
75 $order = (array)$order;
76 foreach ($order as $i => $ord)
77 {
78 $params[] = &$columns[$column][$i];
79 }
80 }
81 unset($order, $column);
82 $params[] = &$array;
83 if($preserveKeys)
84 {
85 $params[] = &$preserveDataKeys;
86 }
87
88 call_user_func_array('array_multisort', $params);
89
90 if($preserveKeys)
91 {
92 $array = array_combine(array_values($preserveDataKeys), array_values($array));
93 }
94 }
95
108 public static function firstNotEmpty()
109 {
110 $argCount = func_num_args();
111 for ($i = 0; $i < $argCount; $i += 2)
112 {
113 $anArray = func_get_arg($i);
114 $key = func_get_arg($i+1);
115 if (is_array($key))
116 {
117 $current = &$anArray;
118 $found = true;
119 foreach ($key as $k)
120 {
121 if (!is_array($current) || !array_key_exists($k, $current))
122 {
123 $found = false;
124 break;
125 }
126 $current = &$current[$k];
127 }
128 if ($found)
129 {
130 if (is_array($current) || is_object($current) || $current != "")
131 return $current;
132 }
133 }
134 elseif (is_array($anArray) && array_key_exists($key, $anArray))
135 {
136 if (is_array($anArray[$key]) || is_object($anArray[$key]) || $anArray[$key] != "")
137 return $anArray[$key];
138 }
139 }
140 return "";
141 }
142
150 public static function normalizeArrayValuesByInt(&$map, $sorted = true)
151 {
152 if (empty($map) || !is_array($map))
153 return;
154
155 $result = array();
156 foreach ($map as $value)
157 {
158 $value = (int)$value;
159 if (0 < $value)
160 $result[$value] = true;
161 }
162 $map = array();
163 if (!empty($result))
164 {
165 $map = array_keys($result);
166 if ($sorted)
167 sort($map);
168 }
169 }
170
177 public static function isAssociative(array $array)
178 {
179 $array = array_keys($array);
180
181 return ($array !== array_keys($array));
182 }
183
191 public static function clone(array $originalArray): array
192 {
193 $clonedArray = [];
194 foreach ($originalArray as $index => $value)
195 {
196 if (is_array($value))
197 {
198 $value = static::clone($value);
199 }
200 elseif (is_object($value))
201 {
202 $value = clone $value;
203 }
204
205 $clonedArray[$index] = $value;
206 }
207
208 return $clonedArray;
209 }
210
218 public static function getByNestedKey(array $array, array $key)
219 {
220 if (empty($key))
221 {
222 return null;
223 }
224
225 $value = $array;
226
227 while (!empty($key))
228 {
229 $subKey = array_shift($key);
230
231 if (array_key_exists($subKey, $value))
232 {
233 $value = $value[$subKey];
234 }
235 else
236 {
237 return null;
238 }
239 }
240
241 return $value;
242 }
243
253 public static function setByNestedKey(array &$array, array $key, $value): void
254 {
255 if (empty($key))
256 {
257 throw new ArgumentException('Empty key to set');
258 }
259
260 $reference =& $array;
261 while (!empty($key))
262 {
263 $subKey = array_shift($key);
264
265 if (!array_key_exists($subKey, $reference))
266 {
267 $reference[$subKey] = [];
268 }
269 $reference = &$reference[$subKey];
270 }
271
272 $reference = $value;
273 unset($reference);
274 }
275
284 public static function unsetByNestedKey(array &$array, array $key): void
285 {
286 if (empty($key))
287 {
288 throw new ArgumentException('Empty key to unset');
289 }
290
291 $reference =& $array;
292 while (!empty($key))
293 {
294 $subKey = array_shift($key);
295
296 if (!array_key_exists($subKey, $reference))
297 {
298 break;
299 }
300
301 if (empty($key))
302 {
303 // last element
304 unset($reference[$subKey]);
305 }
306 else
307 {
308 $reference = &$reference[$subKey];
309 }
310 }
311 }
312}
static unsetByNestedKey(array &$array, array $key)
static setByNestedKey(array &$array, array $key, $value)
static normalizeArrayValuesByInt(&$map, $sorted=true)
static isAssociative(array $array)
static getByNestedKey(array $array, array $key)
static clone(array $originalArray)
static sortByColumn(array &$array, $columns, $callbacks='', $defaultValueIfNotSetValue=null, $preserveKeys=false)