Jump to content

Recommended Posts

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

)

Link to comment
https://forums.phpfreaks.com/topic/294533-sorting-multi-array/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/294533-sorting-multi-array/#findComment-1505464
Share on other sites

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 by Strider64
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/294533-sorting-multi-array/#findComment-1505467
Share on other sites

  • 2 weeks later...

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.

Link to comment
https://forums.phpfreaks.com/topic/294533-sorting-multi-array/#findComment-1506307
Share on other sites

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.

 

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/294533-sorting-multi-array/#findComment-1506337
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.