How to sort an array of names by surname preserving the keys

前端 未结 6 794
一生所求
一生所求 2021-01-05 16:59

I have an array as follows:

Array(
    [27] => \'Sarah Green\',
    [29] => \'Adam Brown\',
    [68] => \'Fred Able\'
);

I\'d like

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 17:30

    I'm not sure if there's a straight-forward way of sorting your example array by surnames.

    You could add names to the array in a different manner. i.e. "Fred Able" would become "Able, Fred". That would allow you to use PHP's inbuilt array-sorting functions.

    Failing that, you might have to go through each array element and explode by whitespace, using the last piece of the returned array as your surname. A bit messy, but I'm sure there are tonnes of examples and functions out there.

提交回复
热议问题