How to sort an array of names by last name, keeping keys

I have an array as follows:

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

I would like to sort it by last name and save the keys:

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

Some names may have more than the first two names, but this is always the very last name I want to sort.

What would be the best way to do this in PHP?

+4
source share
6 answers

You can use the uasort function, which allows you to specify your own sorting method while also saving keys:

 <?php // A function to sort by last name. function lastNameSort($a, $b) { $aLast = end(explode(' ', $a)); $bLast = end(explode(' ', $b)); return strcasecmp($aLast, $bLast); } // The array of data. $array = array( 27 => 'Sarah Green', 29 => 'Adam Brown', 68 => 'Fred Able' ); // Perform the sort: uasort($array, 'lastNameSort'); // Print the result: print_r($array); ?> 

Here is a demo.

+9
source
 function sortByLastName($a){ $tmp = $a; foreach($tmp as $k => $v){ $tmp[$k] = substr($v,strrpos($v, ' ')+1); } asort($tmp); $ret = array(); foreach($tmp as $k => $v){ $ret[$k] = $a[$k]; } return $ret; } 

This may not be the fastest way, but it works. It works if they have middle names.

This is faster than the accepted answer.

http://codepad.org/ogGibRpH

+1
source

I don’t think you could directly do this using the built-in sorting of PHP arrays, although I would be interested if someone fixed me.

The easiest way is to change the names to turn green, Sarah, because then you could use your own PHP sort functions, for example. using asort () (which supports key association).

If this is not possible, you will probably need to implement your own sort function. The easiest way to do this is to use usort (), which allows you to define a custom comparator function. For example:

 usort($arr, "sortByLastName"); function sortByLastName($a, $b) { //DO YOUR COMPARISON IN HERE } 
0
source

Maybe something similar to this (not verified)

 function sortSurname($a,$b){ $sur_a = array_pop(explode(' ',$a)); $sur_b = array_pop(explode(' ',$b)); return $sur_a < $sur_b; } usort($array, 'sortSurname') 
0
source

I'm not sure if there is an easy way to sort your array of examples by last name.

You can add names to the array in another way. that is, "Fred Able" will become "Able, Fred." This will allow you to use the built-in PHP array sorting functions.

Otherwise, you may have to go through each element of the array and explode by spaces, using the last piece of the returned array as your last name. A bit messy, but I'm sure there are tons of examples and functions.

0
source

You might want to create an auxiliary array with last names, save the key, order it, and then restore the original array:

 function order_by_surname($names) { foreach ($names as $key => $name) { $surnames[$key] = array_pop(explode(' ', $name)); } asort($surnames); foreach ($surnames as $key => $surname) { $ordered[$key] = $names[$key]; } return $ordered; 

}

Also note that you may have problems with this because the last name is not always the last word of the full name when you have two last names (e.g. John Clark Moore).

0
source

Source: https://habr.com/ru/post/1397524/


All Articles