Shoutbox

updated - Sorting array data (PHP) - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: updated - Sorting array data (PHP) (/showthread.php?tid=80839)

updated - Sorting array data (PHP) by lordy on 01-10-2008 at 09:48 AM

hey kiddies

what I'm looking to do is this:

say i have a multidimensional array like so:

code:
[0] => {
  ['name'] => "Baby, Please Don't Go"
  ['artist'] => {
                [0]=> "AC/DC"
                }
}
[1] => {
  ['name'] => "Tequila"
  ['artist'] => {
                [0] => "A.L.T. and The Lost Civilization"
                }
}
[2] => {
  ['name'] => "Show You Love Me"
  ['artist'] => {
                [0] => "A.K. Soul"
                [1] => "Jocelyn Brown"
                }
}
[3] => {
  ['name'] => "Dog Eat Dog"
  ['artist'] => {
                [0] => "AC/DC"
                }
}


How can I get it to sort by a specific key, for example the first artist key, alphabetically so it ends up like this:

code:
[0] => {
  ['name'] => "Baby, Please Don't Go"
  ['artist'] => {
                [0]=> "AC/DC"
                }
}
[1] => {
  ['name'] => "Dog Eat Dog"
  ['artist'] => {
                [0] => "AC/DC"
                }
}
[2] => {
  ['name'] => "Show You Love Me"
  ['artist'] => {
                [0] => "A.K. Soul"
                [1] => "Jocelyn Brown"
                }
}
[3] => {
  ['name'] => "Tequila"
  ['artist'] => {
                [0] => "A.L.T. and The Lost Civilization"
                }
}


I hope that is clear enough lol i hope it's possible

btw its in PHP :)
RE: updated - Sorting array data (PHP) by markee on 01-10-2008 at 10:50 AM

Have a look at some of these examples, you should be able to work out how to use it how you want.

http://au2.php.net/manual/en/function.array-multisort.php


RE: updated - Sorting array data (PHP) by lordy on 01-10-2008 at 09:40 PM

I had a look at that and from what i understand of what it does, it actually doesn't so what i want it to, if i undersatnd it correctly that functino will only sort within on branch of the array (ie. if i have multiple [artist] thingies on one particular branch then it will sort within those artists) but it will not rearrange the entire array, which is what i need it to do lol


RE: updated - Sorting array data (PHP) by WDZ on 01-11-2008 at 12:03 AM

array_multisort() should work fine for that. :p Here's an example of how you could use it...

code:
$songs = array(); // Your data would be in here
$artists = array(); // Temporary array

foreach($songs as $key => $song) {
    // Copy the first artist name
    $artists[$key] = $song['artist'][0];
}

array_multisort($artists, $songs);

RE: updated - Sorting array data (PHP) by Adeptus on 01-11-2008 at 04:23 AM

You should also keep in mind that if the data is coming from a database, you can easily tell the database to return it presorted...