0

How to display the following array string in alphabetically without using the collator method

$country = array('Ägypten','Afghanistan', 'Äquatorialguinea', 'Albanien');

My code is:

function compareASCII($a, $b) {
    $a1 = iconv('UTF-8', 'ASCII//TRANSLIT', $a);
    $b1 = iconv('UTF-8', 'ASCII//TRANSLIT', $b);
    return strcmp($a1, $b1);
}

usort($country, 'compareASCII');

Output:

Array ( [0] => Ägypten [1] => Äquatorialguinea [2] => Afghanistan [3] => Albanien )

Expected Output:

 Array ( [0] => Afghanistan [1] => Ägypten [2] => Albanien [3] => Äquatorialguinea )

How to get expected output?

Thanks for advance!!!

vinoth
  • 106
  • 2
  • 12
  • 2
    [How to sort an array of UTF-8 strings](http://stackoverflow.com/questions/120334/how-to-sort-an-array-of-utf-8-strings) – Mark Baker Jan 25 '17 at 12:24

1 Answers1

1

It can made manual (simple version, can be extended):

$country = array('Ägypten','Afghanistan', 'Äquatorialguinea', 'Albanien');

usort ($country,function($a,$b){
    $a=str_replace(array('Ä','Ö','Ü'),array('A','O','U'),$a);
    $b=str_replace(array('Ä','Ö','Ü'),array('A','O','U'),$b);
    return strcmp($a,$b);
});

print_r($country);

//Result
//Array ( [0] => Afghanistan [1] => Ägypten [2] => Albanien [3] => Äquatorialguinea )
JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • @vinoth But read about the diff. between `mb_str_replace` and `str_replace`. It is possible that in some cases the function may not work right. Its all about encoding. But as long this function fits to your belongings take it :-) – JustOnUnderMillions Jan 25 '17 at 12:43