Jump to content


Photo

Sort multidimensional array


  • Please log in to reply
6 replies to this topic

#1 maxodoe

maxodoe
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 May 2006 - 05:12 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 21 May 2006 - 05:50 PM

try

$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 result
echo '<pre>', print_r($scores, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 06:29 PM

if you simply do

sort($scores);

it will sort the array by name automatically.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 maxodoe

maxodoe
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 May 2006 - 09:02 PM

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





#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 10:02 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 maxodoe

maxodoe
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 May 2006 - 08:18 AM

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



#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 22 May 2006 - 07:41 PM

IF the scores and name keys had been in the right order, eg


$scores = array (
    array (
        'score' =>19,
        'name' => 'Michael'
        ),
    array (
        'score' =>15,
        'name' => 'Fred'
        ),
    array (
        'score' =>19,
        'name' => 'Mary'
        )
);

then you could have used this to sort by score then name

array_multisort($scores);

echo '<pre>', print_r($scores, true), '</pre>';
?>
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users