Jump to content

[SOLVED] Sorting a multi-dimensional array


ninedoors

Recommended Posts

I know that there has to be a function out there that sorts multi-dimensional array other than array_multisort. 

 

I pull this data out of a database into an array with the structure:

 

$array[event] = count;

 

where event is an integer for a given time period like the winter season and member is just the member number of one of my members.  So I just increment $array[event] everytime is is pulled out and then I have an array with a bunch of counts.

 

The thing is I don't have how many different events, of members there will be as the user is selecting the data by way of a form.

 

What I need to be able to sort this array by the count value so I would start with something like this:

 

$array[1][120] = 3;

$array[2][120] = 2;

$array[2][151] = 1;

$array[1][153] = 0;

$array[3][162] = 10;

$array[2][12] = 2;

$array[2][1] = 111;

$array[1][34] = 3;

 

And would liekto get this back:

 

$array[2][1] = 111;

$array[3][162] = 10;

$array[2][120] = 3;

$array[2][34] = 3;

$array[1][120] = 2;

$array[2][12] = 2;

$array[2][151] = 1;

$array[1][153] = 0;

 

Maybe I am missing something but I can't get my head around it right now.  Thanks for the help.

 

Nick 

You can't sort like that on arrays. You could do this instead though:

$array["$event_$member"] = $count;

Which would give you:

$array[1][120] = 3;
$array[2_120] = 2;
$array[2_151] = 1;
$array[1_153] = 0;
$array[3_162] = 10;
$array[2_12] = 2;
$array[2_1] = 111;
$array[1_34] = 3;

then sort it descending keeping the indexes:

arsort($array);

then loop over it:

foreach($array as $k=>$count){
  list($event,$member) = explode('_',$k,2);

}

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.