silverglade Posted September 29, 2009 Share Posted September 29, 2009 hi, im confused with this code , i thought that "Grisold" should come first in the output list, and "Champy" should come last since the sort is ascending. because "gary" comes last in the first sort. any help greatly appreciate to enlighten me . <?php $staff["givenname"][0] = "Jason"; $staff["givenname"][1] = "Manny"; $staff["givenname"][2] = "Gary"; $staff["givenname"][3] = "James"; $staff["surname"][0] = "Gilmore"; $staff["surname"][1] = "Champy"; $staff["surname"][2] = "Grisold"; $staff["surname"][3] = "Gilmore"; $res = array_multisort($staff["surname"],SORT_STRING,SORT_ASC, $staff["givenname"],SORT_STRING,SORT_ASC); print_r($staff); ?> output is as follows: Array ( [givenname] => Array ( [0] => Manny [1] => James [2] => Jason [3] => Gary ) [surname] => Array ( [0] => Champy [1] => Gilmore [2] => Gilmore [3] => Grisold ) ) Link to comment https://forums.phpfreaks.com/topic/175958-sorting-array-descendingascending-order-confusion/ Share on other sites More sharing options...
Psycho Posted September 29, 2009 Share Posted September 29, 2009 Your code is sorting by 'surname' first and then by 'givenname'. So, it will be sorted by lastname, firstname is ascending order: a comes before b, b before c, etc. Your results should be: - Champy, Many - Gilmore, James - Gilmore, Jason - Grisold, Gary Link to comment https://forums.phpfreaks.com/topic/175958-sorting-array-descendingascending-order-confusion/#findComment-927166 Share on other sites More sharing options...
silverglade Posted September 29, 2009 Author Share Posted September 29, 2009 thank you, but shouldn't the order be champy, gary gilmore, james gilmore, jason grisold, manny i still dont get it. LOL. sorry. Link to comment https://forums.phpfreaks.com/topic/175958-sorting-array-descendingascending-order-confusion/#findComment-927176 Share on other sites More sharing options...
silverglade Posted September 29, 2009 Author Share Posted September 29, 2009 nevermind i DO get it now lol. ok so it sorts the last names first, then if it hits duplicate last names it sorts the first names? i thought it was sorting both first names and last names and that the first and last names were unrelated. but i still dont get how the first and last names are related because they are in two different arrays arent they? Link to comment https://forums.phpfreaks.com/topic/175958-sorting-array-descendingascending-order-confusion/#findComment-927184 Share on other sites More sharing options...
Psycho Posted September 29, 2009 Share Posted September 29, 2009 Yes, but the function will relate them by the index when you use it in this manner. By doing a multisort on the two arrays the function is associating $staff["givenname"][0] with $staff["surname"][0]. Read the manual for this function. Specifically look at examples #1 and #2 http://us3.php.net/manual/en/function.array-multisort.php Link to comment https://forums.phpfreaks.com/topic/175958-sorting-array-descendingascending-order-confusion/#findComment-927258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.