Jump to content

Assistance with array_multisort() sorting a multi-dimensional array


thepip3r

Recommended Posts

I'm trying to use array_multisort() to sort an array's 3rd dimension by it's 5th dimension.  The code I'm trying is:

 

array_multisort($_SESSION['data']['EPS'], $_SESSION['data']['EPS'][0]['amount'], SORT_NUMERIC, SORT_ASC);

 

and the array looks like:

 

[EPS] => Array
                (
                    [0] => Array
                        (
                            [uts] => 1259744400
                            [amount] => 20.54
                        )

                    [1] => Array
                        (
                            [uts] => 1259748000
                            [amount] => 20.29
                        )

                    [2] => Array
                        (
                            [uts] => 1259751600
                            [amount] => 14.63
                        )

                    [3] => Array
                        (
                            [uts] => 1259755200
                            [amount] => 1.6
                        )

                    [4] => Array
                        (
                            [uts] => 1259672400
                            [amount] => 98.02
                        )

                    [5] => Array
                        (
                            [uts] => 1259676000
                            [amount] => 104.65
                        )

                    [6] => Array
                        (
                            [uts] => 1259679600
                            [amount] => 101.4
                        )

 

and obviously the 1.6 should be first.  please advise...

right... but on the array_multisort() function, it says  "...or a multidimensional array by one or more dimensions" -- so i assumed i could get it done with that....

 

with usort, i'll have to figure out a custom function to get this done with

you could not sort multiple data sets against each other anyway, not without grouping them and sorting the subgroups.

 

unless they were exectly preportional x=ky for example, but then why would you want to sort both?

 

rajiv,

 

that seemed like it was going to work but didn't... the array resorted to:

 

[EPS] => Array
                (
                    [0] => Array
                        (
                            [uts] => 1259755200
                            [amount] => 1.6
                        )

                    [1] => Array
                        (
                            [uts] => 1259751600
                            [amount] => 14.63
                        )

                    [2] => Array
                        (
                            [uts] => 1259748000
                            [amount] => 20.29
                        )

                    [3] => Array
                        (
                            [uts] => 1259744400
                            [amount] => 20.54
                        )

                    [4] => Array
                        (
                            [uts] => 1259737200
                            [amount] => 155.22
                        )

                    [5] => Array
                        (
                            [uts] => 1259733600
                            [amount] => 155.54
                        )

                    [6] => Array
                        (
                            [uts] => 1259730000
                            [amount] => 156.28
                        )

                    [7] => Array
                        (
                            [uts] => 1259726400
                            [amount] => 154.57
                        )

                    [8] => Array
                        (
                            [uts] => 1259722800
                            [amount] => 152.06
                        )

                    [9] => Array
                        (
                            [uts] => 1259719200
                            [amount] => 107.6
                        )

                    [10] => Array
                        (
                            [uts] => 1259715600
                            [amount] => 103.93
                        )

                    [11] => Array
                        (
                            [uts] => 1259712000
                            [amount] => 108.37
                        )

                    [12] => Array
                        (
                            [uts] => 1259708400
                            [amount] => 111.47
                        )

                    [13] => Array
                        (
                            [uts] => 1259704800
                            [amount] => 107.03
                        )

                    [14] => Array
                        (
                            [uts] => 1259701200
                            [amount] => 102.17
                        )

 

any thoughts?

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.