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