Lectrician Posted February 11, 2015 Share Posted February 11, 2015 I have an array as below, and would like to sort it alphabetically by username. I know in the order shown below, asort works, but what if the username key is not the first? Any ideas on the best ways? Thanks :-) usersdata = array( [12345] = array( [username] => "Toby", => "toby@toby.com", [permissions] => "1,2,3,4" ) [12346] = array( [username] => "Tom", => "tom@tom.com", [permissions] => "5,6,7,8" ) [12347] = array( [username] => "Mable", => "mable@olddear.com", [permissions] => "1,2,3,4" ) [12348] = array( [username] => "Twit", => "twit@twit.com", [permissions] => "1,2,3,4,5,6,7" ) ) Quote Link to comment Share on other sites More sharing options...
requinix Posted February 11, 2015 Share Posted February 11, 2015 Two ways. First is array_multisort but it requires a separate array of use the usernames. You pass that array and the original $usersdata and it sorts both at once, in order (so it sorts by username first and by $usersdata second). I don't like array_multisort(). Second is usort. I like usort(). usort($usersdata, function($a, $b) { return strcmp($a["username"], $b["username"]); });The function gets two entries from the array and return 0 for $a after $b, and 0 if they're the same. This happens to match up exactly with how strcmp() works. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted February 11, 2015 Share Posted February 11, 2015 (edited) If the $userdata variable is associative and you need to maintain the keys as they were, you can use the uasort() function <?php $usersdata = array( 12345 => array( 'username' => "Toby", 'email' => "toby@toby.com", 'permissions' => "1,2,3,4" ), 12346 => array( 'username' => "Tom", 'email' => "tom@tom.com", 'permissions' => "5,6,7,8" ), 12347 => array( 'username' => "Mable", 'email' => "mable@olddear.com", 'permissions' => "1,2,3,4" ), 12348 => array( 'username' => "Twit", 'email' => "twit@twit.com", 'permissions' => "1,2,3,4,5,6,7" ) ); echo '<h2>Array as is</h2><pre>'. print_r($usersdata, 1) . '</pre>'; // Sort by Username's Name: uasort($usersdata, function($x, $y) { return strcasecmp($x['username'], $y['username']); }); echo '<h2>Array sorted by username\'s name</h2><pre>' . print_r($usersdata, 1) . '</pre>'; The function can stay the same, but the uasort() function will take into account and maintain the index association of your values. - I know it's being a little bit nit picky but the uasort is better for associative arrays. Edited February 11, 2015 by Strider64 1 Quote Link to comment Share on other sites More sharing options...
Lectrician Posted February 21, 2015 Author Share Posted February 21, 2015 That's great, thank-you. I had kind of got there, but kept trying this and wondered why it would not work: <<INCORRECT CODE>> uasort($usersdata, function($1, $2) { return strcasecmp($1, $2); }); I also left my asort un-commented directy below the new code, so the new code was doing it's job, and then the asort was obliterating it's work! Twit. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 21, 2015 Share Posted February 21, 2015 Maybe because $1 and $2 are not valid variable names? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 21, 2015 Share Posted February 21, 2015 And even if they were, they would be arrays and not string values. I find if difficult to see how you came up with that code after requinix gave you the working solution Two ways.First is array_multisort but it requires a separate array of use the usernames. You pass that array and the original $usersdata and it sorts both at once, in order (so it sorts by username first and by $usersdata second). I don't like array_multisort().Second is usort. I like usort(). usort($usersdata, function($a, $b) { return strcmp($a["username"], $b["username"]); });The function gets two entries from the array and return <0 for sorting $a before $b, >0 for $a after $b, and 0 if they're the same. This happens to match up exactly with how strcmp() works. 1 Quote Link to comment 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.