Jump to content

Custom Sort an array


IchBin

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
                )
            
        )
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.