2

i have some problems with sorting an array.

List

0 => string 'Australien' (length=10)
1 => string 'Belgien' (length=7)
2 => string 'Botswana' (length=8)
3 => string 'Brasilien' (length=9)
4 => string 'Bulgarien' (length=9)
5 => string 'Burma' (length=5)
6 => string 'China' (length=5)
7 => string 'Costa Rica' (length=10)
73 => string 'Ägypten' (length=8)

But Ägypten should be after Australien. I already tried with the Collator class but our client wont install the extension.

4
  • Do you have any other names like "Ägypten" ? Commented Aug 20, 2015 at 8:34
  • 1
    I am sure this link will help you: stackoverflow.com/questions/7929796/… Commented Aug 20, 2015 at 8:38
  • 1
    You didnt read my whole question ? I already tried the Collator. And yes Its more than one. Commented Aug 20, 2015 at 8:56
  • Hi Noaaah...If you read down below the link I sent you it has more than Collator answer. Like the one Narendra wrote. Commented Aug 20, 2015 at 10:39

1 Answer 1

3

You can use setlocale along with first parameter LC_COLLATE and second locale with en_US.utf8 and simply sort using usort along with strcoll try as

setlocale(LC_COLLATE, 'en_US.utf8');
$array = array('Australien','Belgien','Botswana','Brasilien','Bulgarien','Burma','China','Costa Rica','Ägypten');
usort($array, 'strcoll'); 
print_r($array);

Demo

Sign up to request clarification or add additional context in comments.

7 Comments

In this case the locale should probably be de_DE.UTF-8, but yes, this should work.
Thanks for reffering. I'll update it but it'll generate the same as that of shown by OP Check This @deceze
Its wont work... ` setlocale(LC_COLLATE, 'de_DE.UTF-8'); $this->utf8_encode_deep($list); usort($list['continents'], 'strcoll'); die(var_dump($list['countries']));`
What's not working @noaaah did you checked the demo link its working fine
@NarendraSisodia Well idk for me it wont work i put my code in the comment
|

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.