views:

359

answers:

3

Hi,

I develop a ranking system. I got the following array:

[1] => Array
    (
        [botarin - Branding und Kommunikation] => 1
        [Raiffeisen Kredit 2 Go] => 2
    )

[2] => Array
    (
        [Kindersteckdosen] => 1
        [Surf lieber mit bob] => 1
        [Lafarge Imageinserate] => 1
        [MCG Messecongress Graz Inserate] => 1
    )

1,2 is the category id, then there are names of projects and for each project the amount of votes. how can i sort the array, so the category ids stay sorted like this, but the project names rank by the amount of votes descending?

any ideas?

thanks in advance!

A: 

iterate over the array. For each sub array, use asort

timdev
+2  A: 
// $full_array is your array of category ID's with projects/votes as nested arrays

foreach ($full_array as $cat_id => $projects) {
    asort($projects, SORT_NUMERIC);
    $full_array[$cat_id] = $projects;
}

// Each category ID  within $full_array is now sorted
pix0r
hmm that only sorts the cat_id, but the cat_id should stay untouched. only the projectname and its votes should sort by the votes descending!
Clemens
This code will leave cat_id intact - it will sort projects within the cat_id based on # of votes. If you want it descending, pass the flag SORT_DESC to `asort()`.
pix0r
A: 

To sort in descending order use arsort();

$a = array( "a" => 2 );
arsort( $a );
print_r( $a );
Steve Claridge