PHP: Sort Deeply Nested Array

Posted on 2012-09-06
Last Modified: 2012-09-17
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?

    [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

Question by:n00b0101
    1 Comment
    LVL 107

    Accepted Solution

    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.

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now