Hoe kan ik een array sorteren op een custom manier en niet alfabetisch

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Snelle Jaap

Snelle Jaap

28/02/2018 15:22:19
Quote Anchor link
Ik heb een array die ik wil sorteren in een bepaalde volgorde, niet op nummer en niet alfabetisch.

Dit is hoe mijn array er nu uit ziet:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
Array
(
    [0] => Array
        (
            [id] => 29
            [title] => Christien Quint  
            [introtext] =>
Boekhouding


            [fulltext] =>
            [ordering] => 8
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => christien-quint
            [state] => 1
            [item_id] => 29
            [groottelid] => groot
        )

    [1] => Array
        (
            [id] => 28
            [title] => Roel Methorst
            [introtext] =>
Algemeen directeur


            [fulltext] =>
            [ordering] => 9
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => roel-methorst
            [state] => 1
            [item_id] => 28
            [groottelid] => groot
        )

    [2] => Array
        (
            [id] => 30
            [title] => Johan De Troyer  
            [introtext] =>
Erkend adviseur Regio West-Vlaanderen


            [fulltext] =>
            [ordering] => 7
            [images] => {"image_intro":"images\/artikelen\/timber.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => johan-de-troyer
            [state] => 1
            [item_id] => 30
            [groottelid] => klein
        )

    [3] => Array
        (
            [id] => 31
            [title] => Tom Van Peteghem
            [introtext] =>
Erkend adviseur Regio Oost-Vlaanderen


            [fulltext] =>
            [ordering] => 6
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => tom-van-peteghem
            [state] => 1
            [item_id] => 31
            [groottelid] => klein
        )

    [4] => Array
        (
            [id] => 32
            [title] => Jean-Pierre Adams
            [introtext] =>
Erkend adviseur Regio Vlaams & Waals-Brabant


            [fulltext] =>
            [ordering] => 5
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => jean-pierre-adams
            [state] => 1
            [item_id] => 32
            [groottelid] => klein
        )

    [5] => Array
        (
            [id] => 33
            [title] => Michel Ennekens
            [introtext] =>
Erkend adviseur Regio Antwerpen


            [fulltext] =>
            [ordering] => 4
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => michel-ennekens
            [state] => 1
            [item_id] => 33
            [groottelid] => klein
        )

    [6] => Array
        (
            [id] => 34
            [title] => Eef Schuurmans  
            [introtext] =>
Erkend adviseur Erkend adviseur


            [fulltext] =>
            [ordering] => 3
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => eef-schuurmans
            [state] => 1
            [item_id] => 34
            [groottelid] => klein
        )

    [7] => Array
        (
            [id] => 35
            [title] => Edwin Verheul
            [introtext] =>
Hoofduitvoerder Regio Oost & West-Vlaanderen


            [fulltext] =>
            [ordering] => 2
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => edwin-verheul
            [state] => 1
            [item_id] => 35
            [groottelid] => middel
        )

    [8] => Array
        (
            [id] => 36
            [title] => Jorn De Zaaijer
            [introtext] =>
Hoofduitvoerder Regio Antwerpen & Limburg


            [fulltext] =>
            [ordering] => 1
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => jorn-de-zaaijer
            [state] => 1
            [item_id] => 36
            [groottelid] => middel
        )

    [9] => Array
        (
            [id] => 37
            [title] => Remco Mijderwijk
            [introtext] =>
Hoofduitvoerder Regio Vlaams & Waals-Brabant


            [fulltext] =>
            [ordering] => 0
            [images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [alias] => remco-mijderwijk
            [state] => 1
            [item_id] => 37
            [groottelid] => middel
        )

)


Deze array wil ik sorteren op 'groottelid'

Van:

groot
middel
klein

Nu is hij op alfabet gesorteerd. Hoe kan ik dit het beste aanpakken?

Ik kan geen order tabel ofzo toevoegen aan de database dus het zal echt op array niveau moeten gebeuren.
 
PHP hulp

PHP hulp

14/06/2024 16:49:27
 
Thomas van den Heuvel

Thomas van den Heuvel

28/02/2018 16:11:25
Quote Anchor link
Dit doe je doorgaans met usort().

Custom array sortering --> usort().

Trouwens, als je van "groottelid" een aparte tabel maakt met een sorteringskolom kun je hier prima op sorteren, dus zoiets:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
gl_id | gl_name | gl_order
------+---------+---------
    1 |   groot |        3
    2 |   klein |        1
    3 |  middel |        2

Zolang je een "order" kolom hebt boeit het niet hoe de labels luiden, je sorteert simpelweg op de "order" kolom.

Quote:
Ik kan geen order tabel ofzo toevoegen aan de database dus het zal echt op array niveau moeten gebeuren.

Oh, ok, maar anders was het bovenstaande een optie geweest.
Gewijzigd op 28/02/2018 16:23:06 door Thomas van den Heuvel
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.