sort a md array by size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

sort a md array by size

Post by rubberjohn »

ive been trying to sort a multidimensional array so that the internal arrays are in order of their size, biggest first while maintaining their keys

for example

Code: Select all

$matched_users= array (
     "21" =>array(1),   
     "49" =>array(1,4,8,2),
     "39" =>array(1, 2, 3, 4, 5, 6),
     "11" =>array(),  
     "3"   =>array(2,5)  
);
would become

Code: Select all

$matched_users= array (
     "39" =>array(1, 2, 3, 4, 5, 6),
     "49" =>array(1,4,8,2),
     "3"   =>array(2,5),
     "21" =>array(1),
     "11" =>array()
);
the only way i have manged to do this is by using a second 1D array to act as a reference index, like this

Code: Select all

foreach($matched_users as $key => $value){
	
	$matched_order[$key] = count($value);
	
}
and then sort the values of this array to put them in order

before this i was playing about with a foreach with nested if statements but that just made a mess of the array and didn't work properly...

is there a way I can clean this up and sort the actual array itself so i dont have to use the second array?

thanks

rj
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

would array_unshift and array_push work, in other words can I unshift or push a regular array into a multidimensional array?

thanks

rj
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

because you're using named element arrays, no array_unshift() and array_push() will not work for you.

uasort() with count() in a callback may to sort the array.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$matched_users= array (
     "21" =>array(1),   
     "49" =>array(1,4,8,2),
     "39" =>array(1, 2, 3, 4, 5, 6),
     "11" =>array(), 
     "3"   =>array(2,5) 
); 

function compareByCount($a,$b) {
	$aLen = count($a);
	$bLen = count($b);
	return $bLen - $aLen;
}

usort($matched_users,'compareByCount');
print_r($matched_users);

Code: Select all

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [1] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 8
            [3] => 2
        )

    [2] => Array
        (
            [0] => 2
            [1] => 5
        )

    [3] => Array
        (
            [0] => 1
        )

    [4] => Array
        (
        )

)
edit: feyd's right, you should actually use uasort here
Post Reply