• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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