Mijn vraag
[ Voor 97% gewijzigd door Verwijderd op 01-07-2020 08:05 ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
| /** * Returns an inverted version of a two dimensional array * * @param array $table * * @return array */ public static function tableInvert(array $table): array { $invertedTable = []; foreach ($table as $rowKey => $row) { foreach ($row as $colKey => $cell) { if (isset($invertedTable[$colKey]) === false) { $invertedTable[$colKey] = []; } $invertedTable[$colKey][$rowKey] = $cell; } } return $invertedTable; } /** * Sort an multidim array (that has the format of a table). * * @example ArrayStatic/tableSort.php * * @param array $table The table data * @param array $sortOptions * * @return array Sorted table data * @throws \Exception */ public static function tableSort(array $table, array $sortOptions): array { // Invert the tabular data (sorting by column) $invertedTable = self::tableInvert($table); $allKeys = array_keys($invertedTable); $allKeys = array_combine($allKeys, $allKeys); // Sort Expression $sortParams = []; foreach ($sortOptions as $idx => $option) { // Throw an exception when the column is not set if (array_key_exists('column', $option) === false) { throw new \Exception('Options row ' . $idx . ' has no key "column"'); } if (array_key_exists('order', $option) === false) { $option['order'] = self::SORT_ORDER_ASC; } if (array_key_exists('type', $option) === false) { $option['type'] = self::SORT_TYPE_REGULAR; } // If the column does not exist if (key_exists($option['column'], $invertedTable) === false) { throw new \Exception('There is no column with the name \'' . $option['column'] . '\' in the table'); } $sortParams[] = &$invertedTable[$option['column']]; $sortParams[] = $option['order']; $sortParams[] = $option['type']; $allKeys = unset($allKeys[$option['column']]); } // Add not sorted columns because the structure should remain the same foreach ($allKeys as $key) { $sortParams[] = &$invertedTable[$key]; } // Sort the table if (count($sortParams) > 0) { call_user_func_array('array_multisort', $sortParams); } // Rebuild to table and return return self::tableInvert($invertedTable); } |
"Chaos kan niet uit de hand lopen"
[ Voor 34% gewijzigd door Woy op 22-04-2020 11:31 ]
“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”
Apple iPhone 17 LG OLED evo G5 Google Pixel 10 Samsung Galaxy S25 Star Wars: Outlaws Nintendo Switch 2 Apple AirPods Pro (2e generatie) Sony PlayStation 5 Pro
Tweakers is onderdeel van
DPG Media B.V.
Alle rechten voorbehouden - Auteursrecht © 1998 - 2025
•
Hosting door TrueFullstaq