Jump to content

Sort array of objects by date in objects


piznac

Recommended Posts

Hello all!

 

I have this array of objects:

Array
(
    [0] => stdClass Object
        (
            [first_name] => test
            [last_name] => test
            [title] => test
            [id] => 34
            [type] => 4
            [manager] => 4
            [email] => p@yahoo.com
            [date] => 2014-09-21 07:23:12
            [status] => 2
            [approval_date] => 2014-09-21 07:31:10
            [assessment_id] => 1
            [supervisor_approval] => 1
            [manager_approval_date] => 2014-09-21 07:31:27
            [mainmanger] => 3
        )

    [1] => stdClass Object
        (
            [first_name] => test
            [last_name] => test
            [title] => test
            [id] => 34
            [type] => 4
            [manager] => 4
            [email] => p@yahoo.com
            [date] => 2014-09-20 07:39:55
            [status] => 2
            [approval_date] => 2014-09-20 07:40:41
            [assessment_id] => 3
            [supervisor_approval] => 1
            [manager_approval_date] => 2014-09-20 07:41:07
            [mainmanger] => 3
        )

    [2] => stdClass Object
        (
            [first_name] => jimmy john john
            [last_name] => john
            [title] => Lorad and Master
            [id] => 32
            [type] => 4
            [manager] => 3
            [email] => j@j.com
            [date] => 2014-09-21 07:38:50
            [status] => 2
            [approval_date] => 2014-09-19 07:39:25
            [assessment_id] => 2
            [supervisor_approval] => 1
            [manager_approval_date] => 2014-09-19 07:39:25
            [mainmanger] => 0
        )

)

Where "id" = the user ID. 

 

If there are two or more entries for a user, I want to remove all but the most recent by the "approval_date",... So for the above example it would return:

Array
(
    [0] => stdClass Object
        (
            [first_name] => test
            [last_name] => test
            [title] => test
            [id] => 34
            [type] => 4
            [manager] => 4
            [email] => piznac@yahoo.com
            [date] => 2014-09-21 07:23:12
            [status] => 2
            [approval_date] => 2014-09-21 07:31:10
            [assessment_id] => 1
            [supervisor_approval] => 1
            [manager_approval_date] => 2014-09-21 07:31:27
            [mainmanger] => 3
        )

    [1] => stdClass Object
        (
            [first_name] => jimmy john john
            [last_name] => john
            [title] => Lorad and Master
            [id] => 32
            [type] => 4
            [manager] => 3
            [email] => j@j.com
            [date] => 2014-09-21 07:38:50
            [status] => 2
            [approval_date] => 2014-09-19 07:39:25
            [assessment_id] => 2
            [supervisor_approval] => 1
            [manager_approval_date] => 2014-09-19 07:39:25
            [mainmanger] => 0
        )

)

I'm having a hard time wrapping my head around this concept, so I don't have any example code. And I don't really need or expect someone to code this for me,. just a push in the right direction would be awesome.

Link to comment
Share on other sites

 

 

If that data is coming from a database why don't you just query for the most recent record for each user.

Yeah I assumed that would be the first question I would get. I am doing a lot more to this query after the fact when building this array. This particular part is an add on after the fact,.. so Im trying to avoid re-writing everything.

 

 

 

If not, use usort() with a custom function to sort by id ASC, approval_date DESC then loop through the array ignoring all but the first element for each user.

Ok sounds solid,. thank you!

Edited by piznac
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.