Jump to content
piznac

Sort array of objects by date in objects

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.

Share this post


Link to post
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.

 

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.