66
66
&reftitle.examples;
67
67
<para >
68
68
<example >
69
- <title ><function >collator_get_sort_key</function >example</title >
69
+ <title ><function >collator_get_sort_key</function > example</title >
70
70
<programlisting role =" php" >
71
71
<![CDATA[
72
72
<?php
@@ -85,6 +85,76 @@ echo bin2hex($res);
85
85
</screen >
86
86
</example >
87
87
</para >
88
+
89
+ <para >
90
+ <example >
91
+ <title ><function >Collator::getSortKey</function > example with <function >usort</function ></title >
92
+ <programlisting role =" php" >
93
+ <![CDATA[
94
+ <?php
95
+ $data = [
96
+ [ 'name' => '🇳🇱 Derick Rethans', 'linked_account' => 'https://phpc.social/users/derickr' ],
97
+ [ 'name' => 'Elephpant', 'linked_account' => 'https://phpc.social/phpc' ],
98
+ [ 'name' => '🇫🇷 Marcus Bointon', 'linked_account' => 'https://phpc.social/users/Synchro' ],
99
+ ];
100
+
101
+ /* Create the collator */
102
+ $col = new Collator('en');
103
+
104
+ /* Sort upper-case letters before lower-case letters */
105
+ $col->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST);
106
+
107
+ /* Use a user-defined function with sort, that strips out the emojis */
108
+ */
109
+ usort(
110
+ $data,
111
+ function($a, $b) use ($col) {
112
+ /* Remove the character class 'S' (the Symbols), and remove whitespace
113
+ * (with trim) */
114
+ $aName = trim(preg_replace('/\p{S}+/u', '', $a['name']));
115
+ $bName = trim(preg_replace('/\p{S}+/u', '', $b['name']));
116
+
117
+ /* Create the sort key */
118
+ $aKey = $col->getSortKey($aName);
119
+ $bKey = $col->getSortKey($bName);
120
+
121
+ /* Use the sort key to signal which element sorts first */
122
+ return $aKey <=> $bKey;
123
+ }
124
+ );
125
+
126
+ var_dump($data);
127
+ ?>
128
+ ]]>
129
+ </programlisting >
130
+ &example.outputs.similar;
131
+ <screen >
132
+ array(3) {
133
+ [0] =>
134
+ array(2) {
135
+ 'name' =>
136
+ string(25) "🇳🇱 Derick Rethans"
137
+ 'linked_account' =>
138
+ string(33) "https://phpc.social/users/derickr"
139
+ }
140
+ [1] =>
141
+ array(2) {
142
+ 'name' =>
143
+ string(9) "Elephpant"
144
+ 'linked_account' =>
145
+ string(24) "https://phpc.social/phpc"
146
+ }
147
+ [2] =>
148
+ array(2) {
149
+ 'name' =>
150
+ string(25) "🇫🇷 Marcus Bointon"
151
+ 'linked_account' =>
152
+ string(33) "https://phpc.social/users/Synchro"
153
+ }
154
+ }
155
+ </screen >
156
+ </example >
157
+ </para >
88
158
</refsect1 >
89
159
90
160
<refsect1 role =" seealso" >
0 commit comments