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

  • Home
  • SEARCH
  • 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 192773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:27:14+00:00 2026-05-11T16:27:14+00:00

I have a very specific problem here. I have a multi-dimensional array that I

  • 0

I have a very specific problem here. I have a multi-dimensional array that I want to sort first by half-hour time intervals, second by dates. The function array_multisort will not meet my requirements.

Example: I want:

array(array("time"=>"12:15",
            "date"=>"2009-03-24"),
      array("time"=>"12:10",
            "date"=>"2009-03-23"),
      array("time"=>"12:00",
            "date"=>"2009-03-24"),
      array("time"=>"11:30",
            "date"=>"2009-03-24"));

To end up as:

array(array("time"=>"11:30",
            "date"=>"2009-03-24"),
      array("time"=>"12:10",
            "date"=>"2009-03-23"),
      array("time"=>"12:00",
            "date"=>"2009-03-24"),
      array("time"=>"12:15",
            "date"=>"2009-03-24"));

I’ve tried to accomplish this with uksort in combination with my own sorting callback function. This is the code I am currently using:

uksort($myArray, "sortThirties");

function sortThirties($a, $b)
{
    //Get the two times as timestamps relative to today
    $one = strtotime($a['time']);
    $two = strtotime($b['time']);

    //Round them down to the nearest half-hour time
    $one = $one - ($one % 1800);
    $two = $two - ($two % 1800);

    //Return the difference if times are unequal
    //If times are equal, return the difference between dates.
    return ($one == $two ? strcmp($a['date'],$b['date']) : $one - $two);
}

Immediately after running this function, I print out the array with print_r(), and the order of the data appears to be random. What am I missing?

EDIT:
It turns out, the order is completely random. I added this line to the sortThirties function:

echo "<BR>",$a['time']," ",$b['time'];

and all I got was 50 pages of <BR>‘s.

I know the array is correctly structured because this code executed on the very same array gives me the unsorted data:

foreach($myArray AS $a)
{
    echo "<BR>",$a['date']," ",$a['time'];
}

The only thing I can think of is there must be a problem with uksort. Any thoughts?

  • 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-11T16:27:14+00:00Added an answer on May 11, 2026 at 4:27 pm

    The uksort function sorts the array by keys while usort sorts the array by value and your array to be sorted is acctualy:

    $to_sort = array( 
                      0 => array("time"=>"12:15", "date"=>"2009-03-24"),
                      1 => array("time"=>"12:10", "date"=>"2009-03-23"),
                      2 => array("time"=>"12:00", "date"=>"2009-03-24"),
                      3 => array("time"=>"11:30", "date"=>"2009-03-24")
                    );
    

    Do you see the difference? 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should be able to have that - only you're… May 12, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer Does the link have a regular href which will trigger… May 12, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer IF @A > 0 OR @B > 0 OR @C… May 12, 2026 at 8:53 am

Related Questions

I have a very specific html table construct that seems to reveal a Gecko
Ok, so I have a very simple form with next to no logic in
Problem: I have a set of images, lets say 5. There is an structure
Writing a test app to emulate PIO lines, I have a very simple Python/Tk

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.