#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    31
    Rep Power
    2

    Sorting a multi-dimensional array


    So, I have a multi-dimensional array (generated on the fly) that looks like this:
    PHP Code:
    $users = Array ( 
        [
    userid] => Array ( 
            [
    0] => 12
            
    [1] => 13 
            
    [2] => 14
            
    ...
            ) 
            
        [
    username] => Array ( 
            [
    0] => Michael 
            
    [1] => Jason 
            
    [2] => Hannah 
            
    ...
            ) 
            
        [
    role] => Array ( 
            [
    0] => director 
            
    [1] => member 
            
    [2] => reader
            
    ...
            ) 
        ) 
    and I want to be able to sort it by username and role so I can loop through $users and create a sortable table.
    The array is actually populated from several different sources and I can't sort it at the SQL query level.
    I can change the structure of the array, however, if its easier to sort. So long as I can itenerate through it to echo
    a table row for each user.

    Any ideas? I'd really appreciate it!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    Won't array_multisort do the job?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    31
    Rep Power
    2

    close


    First, I love your quote as your signature. Second, I'm kinda new to PHP and didn't know about array_multisort so thank you for teaching me something new.

    Now, this doesn't do exactly what I need. Here's what's really going on. The pm_projects table in my database holds all the info for each project. Including the ids or members, directors and readers of each project like this
    members: 1/username, 2/username, 4/username
    directors: 8/username, 7/username
    etc.

    So, in this script, I pull each of those fields, run it through a foreach loop, explode each one in turn and assign all the info to a new array $users that can then be looped through to display the users. What I want to be able to do is sort that able using ajax.

    I'm using wordpress, so the query is done through a $wpdb class that is already setup and outputs to an associative array.

    But I feel like my problem may be deeper than sorting the array. I may be going about this hole thing the wrong way.

    Here's my abbreviated code:
    PHP Code:
                    $project $args// the project ID
                    
    $users = array(); // Will hold the array with all the users for this project
                    
    global $wpdb;
                    
    $users_db $wpdb->get_row'SELECT directors, members, readers FROM pm_projects WHERE id=' $projectARRAY_A );

                    
    // Fill the $users array
                    
    $i=0;
                    foreach ( 
    $users_db as $role => $user ) {
                        if ( 
    $user != '' ) {
                            
    $user explode(','$user);
                            foreach (
    $user as $this_user) {
                                
    $this_user explode'/'$this_user ); 
                                
    $users['userid'][$i] = $this_user[0];
                                
    $users['username'][$i] = $this_user[1];
                                
    $users['role'][$i] = trim($role's');
                                
    $i++;
                            }
                        }
                    }

                    for(
    $i 0$size count$users['userid'] ); $i $size; ++$i) {
            
    // Build my table with alternating colors for rows

  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    1) If you're storing multiple pieces of data in the same database column, you're doing it wrong. It seems like that's what you're doing, but I may just have misinterpreted.

    2) Use the jquery datatable to display your data and searching/sorting should be taken care of for you without needing a round-trip to the server.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    31
    Rep Power
    2

    Okay, so best practice?


    Yes, I am storing multiple usernames in one column separated by commas that I then explode() after they've been read. I need to be able to know which users are assigned to each project (just the username and userid) by querying the project table. I'd rather not query the entire users table each time. Seems like a lot of querying.

    Is there a better way to get the same effect?

    And, jquery datatable is beautiful. I believe it will do the trick.

    Thank you! I'm am trying to learn best practices here. I wish I could contribute to the forum more.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    If you ever have comma-delimited data in your tables, it's wrong. That's the general rule. Once you know enough to list the 3-4 conditions in which it's not wrong, you won't need general rules anymore anyway.

    The correct way to do this is to have a user-projects crossreference table storing userID and productID. Then join all 3 tables in a single query to get the results you want. It's more complex looking but orders of magnitude faster than doing string parsing in PHP for every row of your table.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    31
    Rep Power
    2

    thank you


    thank you. I will google SQL joining. I really appreciate it!
  14. #8
  15. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Also since you're new to PHP and working with WordPress, you should be aware that WordPress and most of its plugins are very poor examples of how to structure a database and code in general.

    Your array would be easier to sort if you had it indexed by user id first rather than field.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Agreed, wordpress is awful.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    31
    Rep Power
    2
    Originally Posted by E-Oreo
    Your array would be easier to sort if you had it indexed by user id first rather than field.
    Can you show me what you mean? Just an example of the array structure?

    I am actually developing a wordpress plugin. Not one that will be distributed. I own a small media company and we run our website on a wordpress install. We just want a project management system that is integrated into the admin section of our wordpress.
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    He means this:
    PHP Code:
                                $this_user explode'/'$this_user );  
                                
    $users['userid'][$i] = $this_user[0]; 
                                
    $users['username'][$i] = $this_user[1]; 
                                
    $users['role'][$i] = trim($role's'); 
                                
    $i++; 
    Should be something like:
    PHP Code:
    $this_user explode'/'$this_user );  
    $users[$this_user[0]] = array(
        
    'userid' => $this_user[0],
        
    'username' => $this_user[1],
        
    'role' => trim($role's')); 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    31
    Rep Power
    2

    Perfect, thank you


    That is great, thank you. I think I've also decided to reorganize my database. I think I'm doing more work than I have to. I wish I could give something back to the forum, help answer some questions but I'm just not qualified yet.

    Thanks for all the help.

IMN logo majestic logo threadwatch logo seochat tools logo