tags:

views:

37

answers:

1

I have an array whose elements are name, reversed_name, first_initial and second_initial. A typical row is "Aaron Smith", "Smith, Aaron", "a", "s". Each row in the array has a first_initial or second_initial value of "a".

I need to display the rows alphabetically but based on the "a" part, so that either the name or reversed_name will be displayed. An example output would be:

  • Aaron Smith
  • Abbot, Paul
  • Adrian Jones
  • Anita Thompson
  • Atherton, Susan

I really have no idea how to sort the array this way so any help will be much appreciated!

A: 

If you have a custom sort criteria, use usort

function Sorter($e1, $e2)
{
    return strcmp($e1['first_initial'], $e2['first_initial']);
}

usort($array, "Sorter");

strcmp is a convenient function for comparing strings and returning the proper int codes used in sorting.

Tesserex