ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilArrayUtil.php
Go to the documentation of this file.
1 <?php
2 
32 {
38  public static function quoteArray(array $a_array): array
39  {
40  global $DIC;
41 
42  $ilDB = $DIC->database();
43 
44 
45  if (!is_array($a_array) or !count($a_array)) {
46  return ["''"];
47  }
48 
49  foreach ($a_array as $k => $item) {
50  $a_array[$k] = $ilDB->quote($item);
51  }
52 
53  return $a_array;
54  }
55 
60  public static function stripSlashesRecursive($a_data, bool $a_strip_html = true, string $a_allow = ""): array
61  {
62  if (is_array($a_data)) {
63  foreach ($a_data as $k => $v) {
64  if (is_array($v)) {
65  $a_data[$k] = ilArrayUtil::stripSlashesRecursive($v, $a_strip_html, $a_allow);
66  } else {
67  $a_data[$k] = ilUtil::stripSlashes($v, $a_strip_html, $a_allow);
68  }
69  }
70  } else {
71  $a_data = ilUtil::stripSlashes($a_data, $a_strip_html, $a_allow);
72  }
73 
74  return $a_data;
75  }
76 
80  public static function stripSlashesArray(array $a_arr, bool $a_strip_html = true, string $a_allow = ""): array
81  {
82  foreach ($a_arr as $k => $v) {
83  $a_arr[$k] = ilUtil::stripSlashes($v, $a_strip_html, $a_allow);
84  }
85 
86  return $a_arr;
87  }
88 
92  public static function sortArray(
93  array $array,
94  string $a_array_sortby_key,
95  string $a_array_sortorder = "asc",
96  bool $a_numeric = false,
97  bool $a_keep_keys = false
98  ): array {
99  if (!$a_keep_keys) {
100  return self::stableSortArray($array, $a_array_sortby_key, $a_array_sortorder, $a_numeric);
101  }
102 
103  global $array_sortby, $array_sortorder;
104  $array_sortby = $a_array_sortby_key;
105 
106  if ($a_array_sortorder == "desc") {
107  $array_sortorder = "desc";
108  } else {
109  $array_sortorder = "asc";
110  }
111  if ($a_numeric) {
112  if ($a_keep_keys) {
113  uasort($array, [ilArrayUtil::class, "sort_func_numeric"]);
114  } else {
115  usort($array, [ilArrayUtil::class, "sort_func_numeric"]);
116  }
117  } else {
118  if ($a_keep_keys) {
119  uasort($array, [ilArrayUtil::class, "sort_func"]);
120  } else {
121  usort($array, [ilArrayUtil::class, "sort_func"]);
122  }
123  }
124 
125  return $array;
126  }
127 
131  private static function sort_func(array $left, array $right): int
132  {
133  global $array_sortby, $array_sortorder;
134 
135  if (!isset($array_sortby)) {
136  // occurred in: setup -> new client -> install languages -> sorting of languages
137  $array_sortby = 0;
138  }
139 
140  $leftValue = (string) ($left[$array_sortby] ?? '');
141  $rightValue = (string) ($right[$array_sortby] ?? '');
142 
143  // this comparison should give optimal results if
144  // locale is provided and mb string functions are supported
145  if ($array_sortorder === "asc") {
146  return ilStr::strCmp($leftValue, $rightValue);
147  } elseif ($array_sortorder === "desc") {
148  return ilStr::strCmp($rightValue, $leftValue);
149  }
150 
151  return 0;
152  }
153 
157  private static function sort_func_numeric(array $left, array $right): int
158  {
159  global $array_sortby, $array_sortorder;
160 
161  $leftValue = (string) ($left[$array_sortby] ?? '');
162  $rightValue = (string) ($right[$array_sortby] ?? '');
163 
164  if ($array_sortorder === "asc") {
165  return $leftValue <=> $rightValue;
166  } elseif ($array_sortorder === "desc") {
167  return $rightValue <=> $leftValue;
168  }
169 
170  return 0;
171  }
172 
178  private static function mergesort(array &$array, ?callable $cmp_function = null): void
179  {
180  if ($cmp_function === null) {
181  $cmp_function = 'strcmp';
182  }
183  // Arrays of size < 2 require no action.
184  if (count($array) < 2) {
185  return;
186  }
187 
188  // Split the array in half
189  $halfway = intval(count($array) / 2);
190  $array1 = array_slice($array, 0, $halfway);
191  $array2 = array_slice($array, $halfway);
192 
193  // Recurse to sort the two halves
194  ilArrayUtil::mergesort($array1, $cmp_function);
195  ilArrayUtil::mergesort($array2, $cmp_function);
196 
197  // If all of $array1 is <= all of $array2, just append them.
198  if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) {
199  $array = array_merge($array1, $array2);
200  return;
201  }
202 
203  // Merge the two sorted arrays into a single sorted array
204  $array = [];
205  $ptr1 = $ptr2 = 0;
206  while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
207  if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) {
208  $array[] = $array1[$ptr1++];
209  } else {
210  $array[] = $array2[$ptr2++];
211  }
212  }
213 
214  // Merge the remainder
215  while ($ptr1 < count($array1)) {
216  $array[] = $array1[$ptr1++];
217  }
218  while ($ptr2 < count($array2)) {
219  $array[] = $array2[$ptr2++];
220  }
221  }
222 
230  public static function stableSortArray(
231  array $array,
232  string $a_array_sortby,
233  string $a_array_sortorder = "asc",
234  bool $a_numeric = false
235  ): array {
236  global $array_sortby, $array_sortorder;
237 
238  $array_sortby = $a_array_sortby;
239 
240  if ($a_array_sortorder == "desc") {
241  $array_sortorder = "desc";
242  } else {
243  $array_sortorder = "asc";
244  }
245 
246  // Create a copy of the array values for sorting
247  $sort_array = array_values($array);
248 
249  if ($a_numeric) {
250  ilArrayUtil::mergesort($sort_array, [ilArrayUtil::class, "sort_func_numeric"]);
251  } else {
252  ilArrayUtil::mergesort($sort_array, [ilArrayUtil::class, "sort_func"]);
253  }
254 
255  return $sort_array;
256  }
257 }
static sort_func(array $left, array $right)
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
static quoteArray(array $a_array)
Quotes all members of an array for usage in DB query statement.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static sort_func_numeric(array $left, array $right)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
static stableSortArray(array $array, string $a_array_sortby, string $a_array_sortorder="asc", bool $a_numeric=false)
Sort an aray using a stable sort algorithm, which preveserves the sequence of array elements which ha...
static mergesort(array &$array, ?callable $cmp_function=null)
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
static stripSlashesArray(array $a_arr, bool $a_strip_html=true, string $a_allow="")
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)