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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:38:42+00:00 2026-05-23T04:38:42+00:00

How might I sort the following using PHP? (where wed_2_open comes after wed_1_close) I

  • 0

How might I sort the following using PHP? (where wed_2_open comes after wed_1_close)

I have the following JSON data:

"hours": {
  "mon_1_open": 406800,
  "mon_1_close": 437400,
  "tue_1_open": 493200,
  "tue_1_close": 523800,
  "wed_1_open": 579600,
  "wed_1_close": 590400,
  "thu_1_open": 61200,
  "thu_1_close": 91800,
  "fri_1_open": 147600,
  "fri_1_close": 178200,
  "sat_1_open": 237600,
  "sat_1_close": 264600,
  "sun_1_open": 324000,
  "sun_1_close": 345600,
  "wed_2_open": 597600,
  "wed_2_close": 619200
}

Which I then turn into a usable format using JSON_decode:

$obj=json_decode($json);

This is put into a loop to use convert it to HTML:

foreach ($obj->hours as $key => $val) {
    // Turn array items into HTML list items
}

From previous answers it seems like usort may be an answer, but I get errors telling me $obj is an object rather than an array.

Thanks.

  • 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-23T04:38:42+00:00Added an answer on May 23, 2026 at 4:38 am

    Few notes:

    1. To use any of the array functions on the json_decode()ed data, you must pass true as the second parameter which gives you an associative array instead of an object.

    2. Whenever you plan to sort an array, look at this page which helps you decide which of the 12+ array sorting functions you should use.

    Since the desired sorting is not intuitive (sorting by key puts friday on top and close before open) you should define a custom sorting function; use uksort() which allows you to do just that on array keys:

    <?php
    $data = json_decode('{"hours": {
        "mon_1_open": 406800,
        "mon_1_close": 437400,
        "tue_1_open": 493200,
        "tue_1_close": 523800,
        "wed_1_open": 579600,
        "wed_1_close": 590400,
        "thu_1_open": 61200,
        "thu_1_close": 91800,
        "fri_1_open": 147600,
        "fri_1_close": 178200,
        "sat_1_open": 237600,
        "sat_1_close": 264600,
        "sun_1_open": 324000,
        "sun_1_close": 345600,
        "wed_2_open": 597600,
        "wed_2_close": 619200
    }}', true);
    
    echo 'BEFORE ================================' . PHP_EOL;
    print_r($data);
    
    uksort($data['hours'], 'customsort'); 
    
    echo 'AFTER  ================================' . PHP_EOL;
    print_r($data);
    
    function customsort($a, $b) {
        $tr_prefix = array(
            'mon' => 1,
            'tue' => 2,
            'wed' => 3,
            'thu' => 4,
            'fri' => 5,
            'sat' => 6,
            'sun' => 7
        );
        $tr_suffix = array(
            'open'  => 1,
            'close' => 2
        );
        $a = strtr(strtr($a, $tr_prefix), $tr_suffix);
        $b = strtr(strtr($b, $tr_prefix), $tr_suffix);
        return strcmp($a, $b);
    }
    

    Note: my customsort implementation shown above is vary naive. Improvise if necessary.

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

Sidebar

Related Questions

I have this query which might return multiple results. Is it possible to sort
Using C++ and .net I have a stream of data I want to display
I'm using the following code to sort the results in a UIPicker. The results
You might have a set of properties that is used on the developer machine,
I have the following relation in my rails app: genre - has many -
I'm using the following code to fadeOut and fadeIn content divs on a page
I want to sort objects using by one of their attributes. As of now,
This might be a trivial question, but I have not found anything about it,
I've been using SQL Server to store historical time series data for a couple
I'm saving some data using a series of NSDictionaries, stored in an NSMutableArray and

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.