SORTING ISSUE OF COMPLEX ARRAY

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
bhushanahirrao
Forum Newbie
Posts: 5
Joined: Wed Nov 05, 2008 6:39 am

SORTING ISSUE OF COMPLEX ARRAY

Post by bhushanahirrao »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi All,

Please see following array:

Code: Select all

 
Array
(
    [1] => Array
        (
            [HoraXp] => Array
                (
                    [14] => 1
                    [total] => 1
                )
 
        )
 
    [2] => Array
        (
            [SpoiledSan] => Array
                (
                    [15] => 1
                    [22] => 1
                    [total] => 2
                )
 
        )
 
    [3] => Array
        (
            [3b3nJean] => Array
                (
                    [13] => 1
                    [total] => 1
                )
 
        )
 
    [4] => Array
        (
            [roma89] => Array
                (
                    [13] => 1
                    [18] => 1
                    [22] => 1
                    [24] => 2
                    [total] => 5
                )
 
        )
 
    [5] => Array
        (
            [derosz] => Array
                (
                    [13] => 1
                    [total] => 1
                )
 
        )
 
)
 

I want sort this array by [total] in desc order. Can u please help me.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: SORTING ISSUE OF COMPLEX ARRAY

Post by papa »

Give it a first try and show us your code.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: SORTING ISSUE OF COMPLEX ARRAY

Post by pickle »

usort() might be what you need.

Please don't feel you need to SHOUT YOUR SUBJECT IN ALL CAPS!

We can read just fine.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
rbx
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 4:14 pm

Re: SORTING ISSUE OF COMPLEX ARRAY

Post by rbx »

In this case I usually create a 1-dim reference array that contains same keys as top-level keys of original array and values fill with values to be sorted. Then sort this array and you have access-array to your original array that defines desired order. Example, say we that your original array is stored in $orig_array:

Code: Select all

 
foreach($orig_array as $key=>$data)
{
   foreach($data as $props)
  {
     $access_array[$key] = $props['total'];
  };
};
 
arsort($access_array, SORT_NUMERIC);
 
Then $access_array keys are sorted in desired order and you can use it as access to the original array.
Post Reply