Toon posts:

PHP multidimensionale array regels sorteren i.p.v. kolommen

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Mijn vraag

[ Voor 97% gewijzigd door Verwijderd op 01-07-2020 08:05 ]

Alle reacties


Acties:
  • 0 Henk 'm!

  • Diecke
  • Registratie: December 2010
  • Laatst online: 13:23
Kun je dit niet oplossen in de MySQL query dmv een SORT BY op dat veld?

Acties:
  • 0 Henk 'm!

  • storeman
  • Registratie: April 2004
  • Laatst online: 13:11
Wat je zoekt, bestaat niet native in php. Ik heb ooit dit gemaakt: Het komt uit een class, dus je moet het iets aanpassen naar losse functies.

PHP:
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"


Acties:
  • 0 Henk 'm!

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
Ik volg even niet helemaal wat je wil, maar kun je niet gewoon een custom sort function gebruiken en meegeven aan usort?

Die functie kan dan een member van een class zijn die je construct d.m.v. de column waar je op wil sorteren, zodat je niet voor alle columns een separate sort function hoeft te maken.

[ 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.”