Jump to content

[SOLVED] Help sorting a multi dimensional array


SonicUK

Recommended Posts

Ok here's the array:

$DKP_table
{
[1]
    ['id'] = 1
    ['name'] = Treachery
    ['class'] = Warrior
    ['dkp'] = 50

[2]
    ['id'] = 2
    ['name'] = Bob
    ['class'] = Paladin
    ['dkp'] = 35

[3]
    ['id'] = 3
    ['name'] = Hadria
    ['class'] = Druid
    ['dkp'] = 60
}

foreach ($DKP_table as $value)
{
    print the table
}

 

What i need is for the it to be sorted by ['name'] , ['dkp'] or ['class']. Its not really essential just a nice feature. Can this be done?

Link to comment
Share on other sites

Thanks. I had looked at that page briefly before but didnt really understand half of it.. when i looked again i found an example which did what i want:

 

$sort_arr = array();
foreach($DKP_table AS $uniqid => $row){
foreach($row AS $key=>$value){
	$sort_arr[$key][$uniqid] = $value;
}
}

array_multisort($sort_arr['dkp'], SORT_DESC, $DKP_table);

foreach ($DKP_table as $value)
{
    print the table
}

 

This is for a Guild website for world of warcraft btw, and this is my first projecting doing something more than me just muckin around. err the reason i didnt sort directly from sql is because the data is coming from 3 tables. 1 holds the list of players, another holds a list of awards, the 3rd is a linking table which has the id's of which players got which awards. Each table goes into an array, and then it calculates how much each player got in total.

 

Anyway, seems to work fine. I'll be making that code abit more flexable ofc to use on other things.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.