Search code examples
phparraysmultidimensional-arrayarray-multisort

array_multisort() alphabetizing issues with lowercase letters


I have a database of individuals, and needed to sort them alphabetically by last name. Basically:

array_multisort($arr['a'],SORT_ASC,$arr['b'], etc...)

I had initially put in a SORT_STRING after the SORT_ASC, but it didn't seem to make a difference for me, so I dropped it.

Anyway, the alphabetizing worked perfectly, except on a few French names that began with a lowercase "d", eg. "de Toussard". It dumped those names at the very end, after names beginning with "Z".

When I capitalize the "d" it works fine.

Anything I can do to make it work regardless?


Solution

  • As explained in this example http://php.net/manual/en/function.array-multisort.php#example-6112 you should sort by a lowercase copy of the original array. So if you need $arr['a'] from your example to stay untouched do

    $lowercase = array_map('strtolower', $arr['a']);
    

    and then

    array_multisort($lowercase, SORT_ASC, SORT_STRING, $arr['b']);