Jump to content

Sorting Multi Array


Lectrician

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", => "[email protected]", [permissions] => "1,2,3,4" )

  [12346] = array( [username] => "Tom", => "[email protected]", [permissions] => "5,6,7,8" )

  [12347] = array( [username] => "Mable", => "[email protected]", [permissions] => "1,2,3,4" )

  [12348] = array( [username] => "Twit", => "[email protected]", [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' => "[email protected]", 'permissions' => "1,2,3,4" ),
  12346 => array( 'username' => "Tom",  'email' => "[email protected]", 'permissions' => "5,6,7,8" ),
  12347 => array( 'username' => "Mable", 'email' => "[email protected]", 'permissions' => "1,2,3,4" ),
  12348 => array( 'username' => "Twit",  'email' => "[email protected]", '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. 

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.

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.