2

I have a multidimensional array, that I'm sorting alphabetically, but problem is, with danish special characters æ ø å. They should be sorted in that order, but is not returned in that order.

This is my array (part of is removed)

Array
(
    [0] => Array
        (
            [Name] => John
        )

    [1] => Array
        (
            [Name] => Pater
        )

    [2] => Array
        (
            [Name] => Allan

        )
    [3] => Array
        (
            [Name] => Ø test

        )
    [4] => Array
        (
            [Name] => Å test

        )
    [5] => Array
        (
            [Name] => Æ test

        )
)

I'm using this function to sort it

uasort($sorted_region, function($a, $b) {
    $retval = $a['Name'] <=> $b['Name'];
    return $retval;
});

Anyone know, how to sort it, so I get æ ø å in the right order?

I've seen some using e.g.

setlocale(LC_COLLATE, 'da_DK.utf8');
asort($array, SORT_LOCALE_STRING);

But I'm not sure how to implement this in a multidimensional array.

Thanks for any help! :-)

Dyvel
  • 847
  • 1
  • 8
  • 20

2 Answers2

2

Actually this seems to work

setlocale(LC_ALL, 'da_DK.utf8');

usort($sorted_region, function($a, $b) {
    return strcoll($a['Name'], $b['Name']);
});
Dyvel
  • 847
  • 1
  • 8
  • 20
1

check out this post

with this solution, linked by @Sbls in comments on that page. it would need to be modified for your charset but it should work.

function compareASCII($a, $b) {
  $at = iconv('UTF-8', 'ASCII//TRANSLIT', $a);
  $bt = iconv('UTF-8', 'ASCII//TRANSLIT', $b);
  return strcmp($at, $bt);
}
uasort($lang, 'compareASCII');
Community
  • 1
  • 1
digibucc
  • 83
  • 9