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 8208347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:18:35+00:00 2026-06-07T09:18:35+00:00

I am using usort to sort my arrays. I have an array that contains

  • 0

I am using usort to sort my arrays. I have an array that contains warranty dates. And a sort function that sorts it. However the problem I am having is that not all of the items in the array have a warranty. I want to sort the array but always placing the ones with no date at the bottom.

function warrantyA($a, $b){
    if($a->Warranty == "Available" and $b->Warranty == "Available"){
        $a = explode('/', $a->WarrantyDescription);
        $b = explode('/', $b->WarrantyDescription);

        $today = date("d-m-Y");

        $diffa = abs(strtotime($a[0]) - strtotime($today));
        $diffb = abs(strtotime($b[0]) - strtotime($today));

        $a = floor($diffa / 86400 / 30 );
        $b =  floor($diffb / 86400 / 30 );
        return $a - $b;
    }
}

The above is the code I have now.It does not work the way I want it to. Each object has warranty and warranty description. If there is a warranty then it will say “Avaiable”.

The code take the warranty and compares it to today’s date to determine the amount of months left on the warranty and I want to sort it by that number.

How can I make this functional so that any item that has no warranty is placed at the bottom of the array?

  • 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-07T09:18:38+00:00Added an answer on June 7, 2026 at 9:18 am

    Here is a bit of a trick: Turn the results of $a->Warranty=="Available" and $b->Warranty == "Available" into ints ( will be zero or one), then do a similar subtraction to what you have. If a has a warranty and b doesn’t, this will be 1 – 0, or a is greater. If a does not but b does, it will return -1, and 0 if they both don’t.

    $a_has = (int) $a->Warranty == "Available";
    $b_has = (int) $b->Warranty == "Available";
    if($a_has and $b_has){
        // keep your code here
    }
    
    return $a_has - $b_has;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using php function usort to sort an array. The custom php function
I am using this usort function to sort the array by value: function cmp($a,
I am using usort with a user comparison function to sort an array of
I have combined two arrays using array_merge, and now need to sort them alphabetically.
So, I have an array of objects that I want to sort like so:
I have a problem with PHP usort() . Let's suppose I have an array
I have a very specific problem here. I have a multi-dimensional array that I
here is my array, I think i should be using usort but i have
I´m using strcmp in combination with usort in order to sort an array of
I'm using a function i found on php.net(i think) to sort an array based

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.