IchBin Posted July 11, 2010 Share Posted July 11, 2010 I have an array that looks like this with the print_r() function: Array ( [0] => Array ( [username] => IchBin [name] => IchBin [posts] => 12975 [location] => [href] => link here [link] => IchBin [blurb] => [avatar] => Array ( [name] => [image] => [href] => [url] => ) [last_login] => Today at 04:19:43 PM [last_login_timestamp] => 1278886783 [website] => Array ( [title] => [url] => ) [online] => Array ( [is_online] => 1278886783 [text] => [image_href] => link here ) [groupname] => Admin ) [1] => Array ( [username] => Stu [name] => Stu [posts] => 2713 [location] => Laville [href] => link here [link] => Stu [blurb] => [avatar] => Array ( [name] => [image] => [href] => link here [url] => ) [last_login] => Today at 04:07:38 PM [last_login_timestamp] => 1278886058 [website] => Array ( [title] => Our Site [url] => http://www.website.org ) [online] => Array ( [is_online] => 1278886075 [text] => [image_href] => link here ) [groupname] => Support ) ) Each section is associated to a user. In this example you can see there are two users. But I have many more which I am working with (about 12 or so). At the end of each user you can see that they belong to a group where it says [groupname]. I would like to sort my output of this array so that all users who belong to the same group will be grouped together. Is there a way I can custom sort the array to my specification? I'm not sure how I can arrange the array to group those together before I do the output. Any tips would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/ Share on other sites More sharing options...
IchBin Posted July 11, 2010 Author Share Posted July 11, 2010 Ok, so I was a little a miss when I could have been grouping the results in my query. So I now have them grouped, I just need to do some sort of custom sorting on the groups. Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084603 Share on other sites More sharing options...
Daniel0 Posted July 11, 2010 Share Posted July 11, 2010 How does your table look? You can just do this using SQL. Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084604 Share on other sites More sharing options...
IchBin Posted July 11, 2010 Author Share Posted July 11, 2010 Well, here's the SQL if that gives you enough explanation. Let me know if you need the actual structure. SELECT IFNULL(lo.log_time, 0) AS isOnline, IFNULL(a.id_attach, 0) AS ID_ATTACH, a.filename, a.attachment_type, mem.personal_text, mem.avatar, mem.id_member, mem.member_name, mem.real_name, mem.last_login, mem.website_title, mem.website_url, mem.location, mem.posts, mem.id_group FROM ". $db_prefix."members AS mem LEFT JOIN ".$db_prefix."log_online AS lo ON (lo.id_member = mem.id_member) LEFT JOIN ".$db_prefix."attachments AS a ON (a.id_member = mem.id_member) WHERE mem.id_group IN (". implode(",", $groupIds) .") GROUP BY mem.id_group"; Although, I've ran into a weird problem so my query is likely to change. It would seem after adding the group by statement that I'm not getting the results I want. Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084620 Share on other sites More sharing options...
jcbones Posted July 12, 2010 Share Posted July 12, 2010 Why don't you change your array, so the groupname would be the main key. Like: Array ( [Admin] => Array ( [username] => IchBin [name] => IchBin [posts] => 12975 [location] => [href] => link here [link] => IchBin [blurb] => [avatar] => Array ( [name] => [image] => [href] => [url] => ) [last_login] => Today at 04:19:43 PM [last_login_timestamp] => 1278886783 [website] => Array ( [title] => [url] => ) [online] => Array ( [is_online] => 1278886783 [text] => [image_href] => link here ) ) [support] => Array ( [username] => Stu [name] => Stu [posts] => 2713 [location] => Laville [href] => link here [link] => Stu [blurb] => [avatar] => Array ( [name] => [image] => [href] => link here [url] => ) [last_login] => Today at 04:07:38 PM [last_login_timestamp] => 1278886058 [website] => Array ( [title] => Our Site [url] => http://www.website.org ) [online] => Array ( [is_online] => 1278886075 [text] => [image_href] => link here ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084652 Share on other sites More sharing options...
IchBin Posted July 12, 2010 Author Share Posted July 12, 2010 If I do that, how are you proposing the option to sort things? There are about 7 different groupnames. Sorry, I'm just having trouble wrapping my brain around how this can be sorted. I'm trying to figure out a way to sort the array before I do any output. I could of course do an if check to see what the groupname is, but if I can sort the array then there's no need to do any checks. Trying to do less code isn't always the easiest of course. Thanks for the help so far everyone. Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084666 Share on other sites More sharing options...
Psycho Posted July 12, 2010 Share Posted July 12, 2010 SELECT IFNULL(lo.log_time, 0) AS isOnline, IFNULL(a.id_attach, 0) AS ID_ATTACH, a.filename, a.attachment_type, mem.personal_text, mem.avatar, mem.id_member, mem.member_name, mem.real_name, mem.last_login, mem.website_title, mem.website_url, mem.location, mem.posts, mem.id_group FROM ". $db_prefix."members AS mem LEFT JOIN ".$db_prefix."log_online AS lo ON (lo.id_member = mem.id_member) LEFT JOIN ".$db_prefix."attachments AS a ON (a.id_member = mem.id_member) WHERE mem.id_group IN (". implode(",", $groupIds) .") GROUP BY mem.id_group ORDER BY mem.id_group Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084672 Share on other sites More sharing options...
IchBin Posted July 12, 2010 Author Share Posted July 12, 2010 Yeah, that's pretty much where I'm at now mjdamato. Only now my query isn't returning all the results I need. At this point I'll have to come back to this when I've figured that out. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/207452-custom-sort-an-array/#findComment-1084679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.