Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8300037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:24:05+00:00 2026-06-08T16:24:05+00:00

I have an array that contains multiple objects with many properties. I want to

  • 0

I have an array that contains multiple objects with many properties.

I want to sort it in PHP base on two object properties

Here’s a sample array of objects to give you an idea of what data i’m dealing with:

Array (
    [0] => stdClass Object (
        [username] => user98
        [sender_id] => 98
        [date_sent] => 2012-07-25 00:52:11
        [not_read] => 0
    )
    [1] => stdClass Object (
        [username] => user87
        [sender_id] => 87
        [date_sent] => 2012-07-25 00:59:15
        [not_read] => 1
    )
    [2] => stdClass Object (
        [username] => user93
        [sender_id] => 93
        [date_sent] => 2012-07-25 00:52:13
        [not_read] => 2
    )
    [3] => stdClass Object (
        [username] => user5
        [sender_id] => 5
        [date_sent] => 2012-07-25 00:52:16
        [not_read] => 0
    )
)

I need to sort it resulting to this array:

Array (
    [1] => stdClass Object (
        [username] => user87
        [sender_id] => 87
        [date_sent] => 2012-07-25 00:59:15
        [not_read] => 1
    )
    [2] => stdClass Object (
        [username] => user93
        [sender_id] => 93
        [date_sent] => 2012-07-25 00:52:13
        [not_read] => 2
    )
    [3] => stdClass Object (
        [username] => user5
        [sender_id] => 5
        [date_sent] => 2012-07-25 00:52:16
        [not_read] => 0
    )

    [0] => stdClass Object (
        [username] => user98
        [sender_id] => 98
        [date_sent] => 2012-07-25 00:52:11
        [not_read] => 0
    )


)

The sorting is based on the date property and the not_read property of the object, the not_read > 0 is prioritized first in the sorting, then it will look at the date_sent property and sort it on the latest date_sent. Note that it is not based on who has the higher not_read property.

Then those with 0 not_read property will be sorted by latest date_sent.

Can anyone help me with this program?

thanks a lot of looking!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:24:06+00:00Added an answer on June 8, 2026 at 4:24 pm
    function sortByDate($a, $b)
    {
        if($a->not_read > 0 && $b->not_read == 0)
            return -1;
        if($b->not_read > 0 && $a->not_read == 0)
            return 1;
        if ($a->not_read == 0 && $b->not_read == 0 || $a->not_read > 0 && $b->not_read > 0){
            if(strtotime($a->date_sent) > strtotime($b->date_sent))
                return -1;
            if(strtotime($a->date_sent) < strtotime($b->date_sent))
                return 1;
        }
    
        return 0;
    }
    
    usort($array_to_sort, 'sortByDate');
    

    Note: I would of made an edit to Patrick’s but I wasn’t sure if mine even worked. He was on the right track.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Array that contains some elements multiple times. Now I want to
I have an array that contains @sign in the object. I can't access that
I have an array in php that contains all the lines of a text
I have a class that contains multiple user objects and as such has an
I have an array that contains the points for drawing multiple hills. When I
I have a NSDictionary that contains multiple keys with the same name. Here is
I have a hashmap that contains multiple string arrays. I am trying to output
I have an array that contains the RGB colour values for each pixel in
I have an array that contains an array of arrays if that makes any
I have a struct-array that contains details of different reports that can be run.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.