Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

PHP: Sort Deeply Nested Array

Sorting always confuses me.... I've been trying to use usort, but can't seem to get it to sort more than 1 level down.

In the following array example, I'm trying to sort it by hLogged (it won't always be by hLogged, it may also be by logged, remainder, status, gid, etc.).  The usort function I'm trying to implement specifies whether to sort it asc, or desc.  But, it doesn't sort it at this level and the level it does sort it at doesn't respect the sort order...

So, I've been trying to write a custom function that will take three arguments:

(1) key by which to sort
(2) order by which to sort
(3) the array to sort


Does anyone know how to do this, or if it's possible?

Array
(
    [499] => Array
        (
            [2659] => Array
                (
                    [timestamp] => 1342326294
                    [uid] => 499
                    [name] => <span id="uid-499">John Doe</span>
                    [status] => -1
                    [logged] => -1
                    [details] => Array
                        (
                            [0] => Array
                                (
                                    [tDate] => 1342173543
                                    [type] => fer
                                    [grader] => <span id="gid-837">Jane</span>
                                    [gid] => 837
                                    [hLogged] => 5
                                    [notes] => Completed
                                    [added] => 10
                                    [addstatus] => 1
                                    [bid] => 2659
                                )

                        )

                    [remainder] => 0
                )
            [3445] => Array
                (
                    [timestamp] => 1342556650
                    [uid] => 499
                    [name] => <span id="uid-499">Bob Smith</span>
                    [status] => 2
                    [logged] => 7
                    [details] => Array
                        (
                            [0] => Array
                                (
                                    [tDate] => 1342496119
                                    [type] => fer
                                    [grader] => <span id="gid-837">Kevin</span>
                                    [gid] => 837
                                    [hLogged] => 9
                                    [notes] => Completed
                                    [added] => 3
                                    [addstatus] => 2
                                    [bid] => 3445
                                )

                        )

                    [remainder] => 7
                )
               
		)
)

Open in new window

0
n00b0101
Asked:
n00b0101
1 Solution
 
Ray PaseurCommented:
On of the problems with usort() was the lack of arguments.  You could give it an array and a callable function, but the callable function did not get additional input arguments, so you would wind up writing one usort() callable for each field you wanted to sort.

All that is fixed in the Example 4 of the current documentation.  See if that gives you the jump-start you need.
http://php.net/manual/en/function.usort.php

If not, please post the test data in a form I can copy into a PHP script (maybe XML) and I'll try to give it a shot.

Best regards, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now