Bitrix-D7  20.0.0
main/lib/type/collection.php
См. документацию.
1 <?php
2 namespace Bitrix\Main\Type;
3 
4 class Collection
5 {
6  /**
7  * Sorting array by column.
8  * You can use short mode: Collection::sortByColumn($arr, 'value'); This is equal Collection::sortByColumn($arr, array('value' => SORT_ASC))
9  *
10  * Pay attention: if two members compare as equal, their relative order in the sorted array is undefined. The sorting is not stable.
11  *
12  * More example:
13  * Collection::sortByColumn($arr, array('value' => array(SORT_NUMERIC, SORT_ASC), 'attr' => SORT_DESC), array('attr' => 'strlen'), 'www');
14  *
15  * @param array $array
16  * @param string|array $columns
17  * @param string|array $callbacks
18  * @param null $defaultValueIfNotSetValue If value not set - use $defaultValueIfNotSetValue (any cols)
19  * @param bool $preserveKeys If false numeric keys will be re-indexed. If true - preserve.
20  * @throws \Bitrix\Main\ArgumentOutOfRangeException
21  */
22  public static function sortByColumn(array &$array, $columns, $callbacks = '', $defaultValueIfNotSetValue = null, $preserveKeys = false)
23  {
24  //by default: sort by ASC
25  if (!is_array($columns))
26  {
27  $columns = array($columns => SORT_ASC);
28  }
29  $params = $preserveDataKeys = array();
30  $alreadyFillPreserveDataKeys = false;
31  foreach ($columns as $column => &$order)
32  {
33  $callback = null;
34  //this is an array of callbacks (callable string)
35  if(is_array($callbacks) && !is_callable($callbacks))
36  {
37  //if callback set for column
38  if(!empty($callbacks[$column]))
39  {
40  $callback = is_callable($callbacks[$column])? $callbacks[$column] : false;
41  }
42  }
43  //common callback
44  elseif(!empty($callbacks))
45  {
46  $callback = is_callable($callbacks)? $callbacks : false;
47  }
48 
49  if($callback === false)
50  {
51  throw new \Bitrix\Main\ArgumentOutOfRangeException('callbacks');
52  }
53 
54  //this is similar to the index|slice
55  $valueColumn[$column] = array();
56  foreach ($array as $index => $row)
57  {
58  $value = isset($row[$column]) ? $row[$column] : $defaultValueIfNotSetValue;
59  if ($callback)
60  {
61  $value = call_user_func_array($callback, array($value));
62  }
63  $valueColumn[$column][$index] = $value;
64  if($preserveKeys && !$alreadyFillPreserveDataKeys)
65  {
66  $preserveDataKeys[$index] = $index;
67  }
68  }
69  unset($row, $index);
70  $alreadyFillPreserveDataKeys = $preserveKeys && !empty($preserveDataKeys);
71  //bug in 5.3 call_user_func_array
72  $params[] = &$valueColumn[$column];
73  $order = (array)$order;
74  foreach ($order as $i => $ord)
75  {
76  $params[] = &$columns[$column][$i];
77  }
78  }
79  unset($order, $column);
80  $params[] = &$array;
81  if($preserveKeys)
82  {
83  $params[] = &$preserveDataKeys;
84  }
85 
86  call_user_func_array('array_multisort', $params);
87 
88  if($preserveKeys)
89  {
90  $array = array_combine(array_values($preserveDataKeys), array_values($array));
91  }
92  }
93 
94  /**
95  * Takes all arguments by pairs..
96  * Odd arguments are arrays.
97  * Even arguments are keys to lookup in these arrays.
98  * Keys may be arrays. In this case function will try to dig deeper.
99  * Returns first not empty element of a[k] pair.
100  *
101  * @param array $a array to analyze
102  * @param string|int $k key to lookup
103  * @param mixed $a,... unlimited array/key pairs to go through
104  * @return mixed|string
105  */
106  public static function firstNotEmpty()
107  {
108  $argCount = func_num_args();
109  for ($i = 0; $i < $argCount; $i += 2)
110  {
111  $anArray = func_get_arg($i);
112  $key = func_get_arg($i+1);
113  if (is_array($key))
114  {
115  $current = &$anArray;
116  $found = true;
117  foreach ($key as $k)
118  {
119  if (!is_array($current) || !array_key_exists($k, $current))
120  {
121  $found = false;
122  break;
123  }
124  $current = &$current[$k];
125  }
126  if ($found)
127  {
128  if (is_array($current) || is_object($current) || $current != "")
129  return $current;
130  }
131  }
132  elseif (is_array($anArray) && array_key_exists($key, $anArray))
133  {
134  if (is_array($anArray[$key]) || is_object($anArray[$key]) || $anArray[$key] != "")
135  return $anArray[$key];
136  }
137  }
138  return "";
139  }
140 
141  /**
142  * Convert array values to int, return unique values > 0. Optionally sorted array.
143  *
144  * @param array &$map Array for normalize.
145  * @param bool $sorted If sorted true, result array will be sorted.
146  * @return void
147  */
148  public static function normalizeArrayValuesByInt(&$map, $sorted = true)
149  {
150  if (empty($map) || !is_array($map))
151  return;
152 
153  $result = array();
154  foreach ($map as $value)
155  {
156  $value = (int)$value;
157  if (0 < $value)
158  $result[$value] = true;
159  }
160  $map = array();
161  if (!empty($result))
162  {
163  $map = array_keys($result);
164  if ($sorted)
165  sort($map);
166  }
167  }
168 
169  /**
170  * Check array is associative.
171  *
172  * @param $array - Array for check.
173  * @return bool
174  */
175  public static function isAssociative(array $array)
176  {
177  $array = array_keys($array);
178 
179  return ($array !== array_keys($array));
180  }
181 }
Bitrix\Sender\Connector\$callbacks
$callbacks
Definition: resultview.php:30
Bitrix\Main\Type\Collection\normalizeArrayValuesByInt
static normalizeArrayValuesByInt(&$map, $sorted=true)
Convert array values to int, return unique values > 0.
Definition: main/lib/type/collection.php:148
Bitrix\Main\ORM\Objectify\Collection
Definition: main/lib/orm/objectify/collection.php:32
Bitrix\Main\Type
Definition: main/lib/type/collection.php:2
Bitrix\Main\Type\Collection\isAssociative
static isAssociative(array $array)
Check array is associative.
Definition: main/lib/type/collection.php:175
Bitrix\Main\Type\Collection\sortByColumn
static sortByColumn(array &$array, $columns, $callbacks='', $defaultValueIfNotSetValue=null, $preserveKeys=false)
Sorting array by column.
Definition: main/lib/type/collection.php:22
Bitrix\Main\Type\Collection\firstNotEmpty
static firstNotEmpty()
Takes all arguments by pairs.
Definition: main/lib/type/collection.php:106