in Education by
I currentyl have no clue on how to sort an array which contains UTF-8 encoded strings in PHP. The array comes from a LDAP server so sorting via a database (would be no problem) is no solution. The following does not work on my windows development machine (although I'd think that this should be at least a possible solution): $array=array('Birnen', 'Äpfel', 'Ungetüme', 'Apfel', 'Ungetiere', 'Österreich'); $oldLocal=setlocale(LC_COLLATE, "0"); var_dump(setlocale(LC_COLLATE, 'German_Germany.65001')); usort($array, 'strcoll'); var_dump(setlocale(LC_COLLATE, $oldLocal)); var_dump($array); The output is: string(20) "German_Germany.65001" string(1) "C" array(6) { [0]=> string(6) "Birnen" [1]=> string(9) "Ungetiere" [2]=> string(6) "Äpfel" [3]=> string(5) "Apfel" [4]=> string(9) "Ungetüme" [5]=> string(11) "Österreich" } This is complete nonsense. Using 1252 as the codepage for setlocale() gives another output but still a plainly wrong one: string(19) "German_Germany.1252" string(1) "C" array(6) { [0]=> string(11) "Österreich" [1]=> string(6) "Äpfel" [2]=> string(5) "Apfel" [3]=> string(6) "Birnen" [4]=> string(9) "Ungetüme" [5]=> string(9) "Ungetiere" } Is there a way to sort an array with UTF-8 strings locale aware? Just noted that this seems to be PHP on Windows problem, as the same snippet with de_DE.utf8 used as locale works on a Linux machine. Nevertheless a solution for this Windows-specific problem would be nice... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
$a = array( 'Кръстев', 'Делян1', 'делян1', 'Делян2', 'делян3', 'кръстев' ); $col = new \Collator('bg_BG'); $col->asort( $a ); var_dump( $a ); Prints: array 2 => string 'делян1' (length=11) 1 => string 'Делян1' (length=11) 3 => string 'Делян2' (length=11) 4 => string 'делян3' (length=11) 5 => string 'кръстев' (length=14) 0 => string 'Кръстев' (length=14) The Collator class is defined in PECL intl extension. It is distributed with PHP 5.3 sources but might be disabled for some builds. E.g. in Debian it is in package php5-intl . Collator::compare is useful for usort.

Related questions

0 votes
    I moved a web app that was using 1.8.7 to 1.9.2 and now I keep getting incompatible character ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a Post model that has a likes array that contains the ObjectId of the user that liked the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Which of these method of Array class is used sort an array or its subset? (a) binarysort() (b) ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these method of Array class is used sort an array or its subset? (a) binarysort() (b) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What are the advantages of using an array of pointers to string instead of an array of strings?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    How will you sort a list of string using Java 8 lambda expression?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you print count of empty strings in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Here is my data: { { "_id" : ObjectId("623a92139e19f99295167786"), "headline" : "headline1", " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm trying to import an XML file via a web page in a Ruby on Rails application, the code ruby ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    _______ is the sort directly applied on an axis using the sort dialog button. Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    The following script both demonstrates and documents (in the header comment) the issue, namely that I am unable to ... any difference between the "expected" and "actual" strings:...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I want to disable the sort property in dataTable API. I use bsort:false to disable sorting from all ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
...