0

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!

flag

3 Answers

2
// $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
link|flag
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 Oct 8 '09 at 8:00
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 Oct 8 '09 at 18:14
0

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

link|flag
0

To sort in descending order use arsort();

$a = array( "a" => 2 );
arsort( $a );
print_r( $a );
link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.