maxodoe Posted May 21, 2006 Share Posted May 21, 2006 I need to sort the following array, first by score then by name. I am just starting to learn PHP and everything worked just fine until this. Can anyone help me?$scores[0]['name'] = 'Michael';$scores[0]['score'] = 13;$scores[1]['name'] = 'Fred';$scores[1]['score'] = 15;$scores[2]['name'] = 'Mary';$scores[2]['score'] = 19;/Max Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/ Share on other sites More sharing options...
Barand Posted May 21, 2006 Share Posted May 21, 2006 try[code]$scores[0]['name'] = 'Michael';$scores[0]['score'] = 19;$scores[1]['name'] = 'Fred';$scores[1]['score'] = 15;$scores[2]['name'] = 'Mary';$scores[2]['score'] = 19;function score_name_sort($a, $b) { if ($a['score'] == $b['score']) { return strcmp($a['name'], $b['name']); } return $a['score'] < $b['score'] ? -1 : 1;}usort ($scores, 'score_name_sort');// check resultecho '<pre>', print_r($scores, true), '</pre>';[/code] Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/#findComment-37736 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 if you simply do sort($scores);it will sort the array by name automatically. Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/#findComment-37754 Share on other sites More sharing options...
maxodoe Posted May 21, 2006 Author Share Posted May 21, 2006 I had been working on this for days but couldn't make it work.But your usort solution worked like a clock and solved everything.A million thanks!!!!About the sort($scores); function:Is there any way to change the key by which it sorts?Again thank you so much for your help!/Max Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/#findComment-37788 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 nope, not that i've been able to determine. the way the sort function works is it sorts the first array sets first and then after that it looks at the 2nd array, etc.. so you would think since it does look at more than one anyways, there would be a way to do like a sort($array, $level) or something built in, but.. nope, not that i have been able to find. You gotta do a traditional sorting method like bubble sort or something, like how barand did. Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/#findComment-37792 Share on other sites More sharing options...
maxodoe Posted May 22, 2006 Author Share Posted May 22, 2006 Now I understand how to use the sort function. And the usort solution works fine when I want to use a second key as well. I almost gave up, but now I'm a happy man.Thank you very much for explaining everything! /Max Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/#findComment-37898 Share on other sites More sharing options...
Barand Posted May 22, 2006 Share Posted May 22, 2006 IF the scores and name keys had been in the right order, eg[code]$scores = array ( array ( 'score' =>19, 'name' => 'Michael' ), array ( 'score' =>15, 'name' => 'Fred' ), array ( 'score' =>19, 'name' => 'Mary' ));[/code]then you could have used this to sort by score then name[code]array_multisort($scores);echo '<pre>', print_r($scores, true), '</pre>';[/code]?> Link to comment https://forums.phpfreaks.com/topic/10132-sort-multidimensional-array/#findComment-38026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.