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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:25:22+00:00 2026-05-25T20:25:22+00:00

Was wondering if there is a way to sort (usort or equiv) an array

  • 0

Was wondering if there is a way to sort (usort or equiv) an array based on key order of another array and date. I know how to do basic date sorting with usort, but was wondering if you could do both in one shot.

$cats = array(1,6,3,4,7,2);
$prods = array(
 array('cat'=>1,'date'=>'1/3/2011'),
 array('cat'=>2,'date'=>'1/6/2011'),
 array('cat'=>3,'date'=>'2/3/2011')
);

I want $prods sorted by the category order of $cats and date order of $prods

  • 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-05-25T20:25:23+00:00Added an answer on May 25, 2026 at 8:25 pm

    Pay attention to your low accept rate, as it can deter people from taking the time to answer your questions. Remember to mark valid answers as accepted or otherwise post a comment to them explaining why they aren’t valid.

    This solution works fine in php 5.3+, if you are trapped in an older version you can easily replace DateTime objects with calls to strptime.

    Products with a category that has no position specified are pushed to the end of the list (see product with cat == 42 in the example).

    $cats = array(1,6,3,4,7,2);
    
    // added a few products to test the date sorting
    $prods = array(
      array('cat'=>1,'date'=>'3/3/2011'),
      array('cat'=>1,'date'=>'2/3/2011'),
      array('cat'=>1,'date'=>'1/3/2011'),
      array('cat'=>42,'date'=>'2/3/2011'),
      array('cat'=>2,'date'=>'1/3/2011'),
      array('cat'=>2,'date'=>'2/3/2011'),
      array('cat'=>2,'date'=>'1/6/2011'),
      array('cat'=>3,'date'=>'2/3/2011')
    );
    
    // need an index of category_id => position wanted in the sort
    $r_cats = array_flip($cats);
    // if the category of a product is not found in the requested sort, we will use 0, and product with category id 0 will be sent to the end of the sort
    $r_cats[0] = count($r_cats);
    // this one is needed for DateTime, put whatever your timezone is (not important for the use we do of it, but required to avoid a warning)
    date_default_timezone_set('Europe/Paris');
    
    usort($prods, function($a, $b) use($r_cats) {
      $cat_a = isset($r_cats[$a['cat']]) ? $r_cats[$a['cat']] : $r_cats[0];
      $cat_b = isset($r_cats[$b['cat']]) ? $r_cats[$b['cat']] : $r_cats[0];
    
      if ($cat_a < $cat_b) {
        return -1;
      } elseif ($cat_a > $cat_b) {
        return 1;
      }
    
      $date_a = DateTime::createFromFormat('j/n/Y', $a['date']);
      $date_b = DateTime::createFromFormat('j/n/Y', $b['date']);
    
      if ($date_a < $date_b) {
        return -1;
      } elseif ($date_a > $date_b) {
        return 1;
      }
    
      return 0;
    });
    
    var_dump($prods);
    

    The results are as follow:

    array(8) {
      [0]=>
      array(2) {
        ["cat"]=>
        int(1)
        ["date"]=>
        string(8) "1/3/2011"
      }
      [1]=>
      array(2) {
        ["cat"]=>
        int(1)
        ["date"]=>
        string(8) "2/3/2011"
      }
      [2]=>
      array(2) {
        ["cat"]=>
        int(1)
        ["date"]=>
        string(8) "3/3/2011"
      }
      [3]=>
      array(2) {
        ["cat"]=>
        int(3)
        ["date"]=>
        string(8) "2/3/2011"
      }
      [4]=>
      array(2) {
        ["cat"]=>
        int(2)
        ["date"]=>
        string(8) "1/3/2011"
      }
      [5]=>
      array(2) {
        ["cat"]=>
        int(2)
        ["date"]=>
        string(8) "2/3/2011"
      }
      [6]=>
      array(2) {
        ["cat"]=>
        int(2)
        ["date"]=>
        string(8) "1/6/2011"
      }
      [7]=>
      array(2) {
        ["cat"]=>
        int(42)
        ["date"]=>
        string(8) "2/3/2011"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there is way to open another page using a Modal
I was wondering if there's a way to sort columns with Table sorter So
I was wondering what would be the fastest way to sort an array of
I'm wondering if there is a way to do a numeric sort when the
I'm pretty sure I know the answer but I'm wondering if there's a way
I'm wondering if there's any way of having some sort of Aspect-Oriented way of
I was wondering is there a way in PHP that you could tell where
I am wondering is there a way to render a partial view in the
I am wondering is there any way we can test the font size/color of
I think I understand unit testing. But I was wondering: is there a way

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.