0

I'm having multi-dimensional problem :) I have an array, which contains this:

array (33)
18 => "D3 0309/II Ševětín – Borek" (31)
19 => "D3 0311 TŘEBONÍN – KAPLICE NÁDRAŽÍ" (41)
25 => "D3 0312/I Kaplice nádraží - Nažidla" (39)
26 => "D3 0312/II - Nažidla - Dolní Dvořiště státní hranice" (59)
27 => "D3 TEST" (7)
15 => "D30310_Hodějovice-Třebonín" (29)
30 => "D4 Lety - Čimelice" (19)
29 => "D4 Milín - Lety" (16)
32 => "D4 Mirotice rozšíření" (25)
20 => "D4 křižovatka II/118 – Milín, DSP" (38)
31 => "D4 Čimelice - Mirotice" (23)
13 => "D6 Lubenec obchvat 1.etapa" (26)
23 => "D6 Nové Strašecí-Řevničov" (30)
6 => "D6 Řevničov obchvat" (21)
33 => "D8 MÚK Zdiby – rozšíření Prosecké radiály, etapa 2 – direktní větev" (80)
12 => "I/21 Trstěnice - Drmoul" (24)
2 => "I/37 Chrudim - obchvat, úsek křiž. I/17 - Slatiňany" (55)
44 => "Jirka (ostatni nesahat) - import Valbek" (39)
17 => "Klatovy" (7)
37 => "Letiste Pribram" (15)
24 => "Optimalizace traťového úseku Mstětice – Praha - Vysočany" (63)
34 => "Radost II" (9)
7 => "SOKP 512 "D1-Jesenice - Vestec" Psáry - přeložka silnice II/105" (66)
14 => "Stavba - návod" (15)
43 => "Test ě+ščřžýáíé0123465789 sss" (38)
4 => "Testovací stavba" (17)
39 => "Videnska" (8)
38 => "Vratislavice" (12)
36 => "Zvýšení kapacity trati Nymburk – Mladá Boleslav, 2.stavba" (63)
41 => "jirka test 4" (12)
22 => "test" (4)
35 => "test-D4 křižovatka II/118 – Milín" (38)
45 => "čtest" (6)

At first, I must keep indexes, because they are taken in select query as ID. Second, I have to sort that case-insensitive and third, I need to sort it with czech encoding. I've searched for some help, but nothing worked for me. I will include my current code, which uses uasort with strcoll , but it sorts case-sensitive and doesn't even work for czech encoding...

setlocale(LC_COLLATE, 'cs_CZ.utf8');
uasort($options, 'strcoll');

also tried this, but with same result...

setlocale(LC_COLLATE, 'cs_CZ.utf8');
asort($options, SORT_LOCALE_STRING);

2 Answers 2

3

So I finally solved it with the way I wanted to avoid.

$sort = function ($a, $b){
    static $czechCharsS = array('Á', 'Č', 'Ď', 'É', 'Ě' , 'Ch' , 'Í', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů' , 'Ý', 'Ž', 'á', 'č', 'ď', 'é', 'ě' , 'ch' , 'í', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů' , 'ý', 'ž');
    static $czechCharsR = array('AZ','CZ','DZ','EZ','EZZ','HZZZ','IZ','NZ','OZ','RZ','SZ','TZ','UZ','UZZ','YZ','ZZ','az','cz','dz','ez','ezz','hzzz','iz','nz','oz','rz','sz','tz','uz','uzz','yz','zz');

    $A = str_replace($czechCharsS, $czechCharsR, $a);
    $B = str_replace($czechCharsS, $czechCharsR, $b);

    return strnatcasecmp($A, $B);
};
uasort($options, $sort);
Sign up to request clarification or add additional context in comments.

Comments

0

Write a sort function and lower first.

setlocale(LC_COLLATE, 'cs_CZ.utf8');
function sortme ($a,$b){
    $a = mb_strtolower($a);
    $b = mb_strtolower($b);
    return strcoll($a,$b);
}

$options = array('Ša','šb','Šc','šd');
uasort($options, 'sortme');
print_r($options);

//result with strtolower
//Array ( [0] => Ša [1] => šb [2] => Šc [3] => šd )

//result without mb_strtolower
//Array ( [0] => Ša [2] => Šc [1] => šb [3] => šd )

4 Comments

That works as case-insensitive, but unfortunately čtest is still last item in array, although it should be first..
after edit: but when you add something without czech encoing, like for example aa, bb, zz, yy, items with special characters are still at last positions..
mmh, that wired. have to look more into it
solved, look at my answer, I've searched through some czech forums, and this seems to be the only solution unfortunately..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.