piznac Posted September 21, 2014 Share Posted September 21, 2014 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] => [email protected] [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] => [email protected] [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] => [email protected] [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] => [email protected] [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] => [email protected] [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 https://forums.phpfreaks.com/topic/291202-sort-array-of-objects-by-date-in-objects/ Share on other sites More sharing options...
Barand Posted September 21, 2014 Share Posted September 21, 2014 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. Link to comment https://forums.phpfreaks.com/topic/291202-sort-array-of-objects-by-date-in-objects/#findComment-1491720 Share on other sites More sharing options...
piznac Posted September 21, 2014 Author Share Posted September 21, 2014 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! Link to comment https://forums.phpfreaks.com/topic/291202-sort-array-of-objects-by-date-in-objects/#findComment-1491721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.