What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » updated - Sorting array data (PHP)

updated - Sorting array data (PHP)
Author: Message:
lordy
Senior Member
****


Posts: 853
Reputation: 24
34 / Male / Flag
Joined: Jul 2004
Status: Away
O.P. updated - Sorting array data (PHP)
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 :)

This post was edited on 01-10-2008 at 09:56 AM by lordy.
01-10-2008 09:48 AM
Profile E-Mail PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: updated - Sorting array data (PHP)
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
[Image: markee.png]
01-10-2008 10:50 AM
Profile PM Find Quote Report
lordy
Senior Member
****


Posts: 853
Reputation: 24
34 / Male / Flag
Joined: Jul 2004
Status: Away
O.P. RE: updated - Sorting array data (PHP)
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
01-10-2008 09:40 PM
Profile E-Mail PM Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: updated - Sorting array data (PHP)
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);
01-11-2008 12:03 AM
Profile PM Web Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: updated - Sorting array data (PHP)
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...
01-11-2008 04:23 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On