The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP5 - Sorting a multi-dimensional array
Discuss Sorting a multi-dimensional array in the PHP Development forum on Dev Shed. Sorting a multi-dimensional array PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 20th, 2012, 11:59 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 20
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
|
|
|
PHP5 - 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!
|

December 20th, 2012, 12:03 PM
|
|
|
|
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

December 20th, 2012, 12:16 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 20
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
|
|
|
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=' . $project, ARRAY_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
}
|

December 20th, 2012, 12:23 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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.
|

December 20th, 2012, 12:29 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 20
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
|
|
|
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.
|

December 20th, 2012, 12:35 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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.
|

December 20th, 2012, 12:37 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 20
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
|
|
|
thank you
thank you. I will google SQL joining. I really appreciate it!
|

December 20th, 2012, 12:45 PM
|
 |
Lost in code
|
|
|
|
|
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.
|

December 20th, 2012, 01:05 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Agreed, wordpress is awful.
|

December 20th, 2012, 01:38 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 20
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
|
|
Quote: | 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.
|

December 20th, 2012, 02:12 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
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'));
|

December 20th, 2012, 02:15 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 20
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|