Search code examples
phparrayssorting

sorting array of names in php


I have an array of people's names:

Array
(
    [1] => A. Aitken
    [2] => A. C. Skinner
    [3] => A. Chen
    [4] => R. Baxter
)

What's the quickest way to sort the array in (alphabetical) order of the surname in php? i.e. to give

Array
(
    [1] => A. Aitken
    [4] => R. Baxter
    [3] => A. Chen
    [2] => A. C. Skinner
)

Solution

  • Have a look at uksort and the example given there, which is very similar to your problem.

    You may want to replace the regexps there with

    preg_replace("/[A-Z]\\. /", '', $a);