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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:32:01+00:00 2026-05-26T15:32:01+00:00

I use array_multisort() function to sort an array by a field value. what I

  • 0

I use array_multisort() function to sort an array by a field value. what I need is to sort it by 2 field values, by date and by time.
here is the structure of the array:

Array
(
    [0] => Array
        (
            [id_art] => 5292
            [free_art] => 0
            [apero_art] => 0
            [name_cat] => Teatro
            [date_dat] => 2010-11-24
            [date2_dat] => 0000-00-00
            [name_spa] => Cinema Teatro
            [title_int] => Il piacere dell'onestà 
            [id_cat] => 2
            [time_tim] => 20:30:00
            [intro_int] => Una produzione Teatro Eliseo di Roma - ChiassoCultura 
            [image_art] => noimage.png
        )

    [1] => Array
        (
            [id_art] => 4983
            [free_art] => 0
            [apero_art] => 0
            [name_cat] => Cinema
            [date_dat] => 2011-04-20
            [date2_dat] => 2011-04-20
            [name_spa] => Cinema Morettina
            [title_int] => Inland Empire
            [id_cat] => 1
            [time_tim] => 20:30:00
            [intro_int] => Rassegna dedicata a David Lynch
            [image_art] => noimage.png
        )

    [2] => Array
        (
            [id_art] => 4983
            [free_art] => 0
            [apero_art] => 0
            [name_cat] => Cinema
            [date_dat] => 2011-04-22
            [date2_dat] => 2011-04-22
            [name_spa] => Cinema Iride
            [title_int] => Inland Empire
            [id_cat] => 1
            [time_tim] => 17:00:00
            [intro_int] => Rassegna dedicata a David Lynch
            [image_art] => noimage.png
        )....

What I do now is:

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }
    array_multisort($sort_col, $dir, $arr); 
}

How can I easily make in one time the double sort?

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-26T15:32:01+00:00Added an answer on May 26, 2026 at 3:32 pm

    If I understand what you need to do, you can use usort:

    usort($array, function($a, $b) {
        if ($a['date_cat'] > $b['date_cat']) return 1;
        if ($a['date_cat'] < $b['date_cat']) return -1;
        if ($a['time_tim'] > $b['time_tim']) return 1;
        if ($a['time_tim'] < $b['time_tim']) return -1;        
        return 0;
    });
    

    The way your function works it a bit twisty, to me.
    You extract a column of your array to create an array to sort linked to the original array. It took several readings and a php manual check to understand your code (too bad), you should have (to me) detach the creation of the index array into another function prior than calling the sort.

    If you need to have dynamic colums selection maybe you can leverage on closures (PHP 5.3+ ?):

    function your_sort($orderby, $array) {
        return usort($array, function ($a, $b) use ($orderby) {
            foreach($orderby as $field) {
                if ($a[$field] > $b[$field]) return 1;
                if ($a[$field] < $b[$field]) return -1;
            }
            return 0;
        });
    }
    

    And then you can call the function like this:

    your_sort(array('date_cat','time_tim'), $array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project where I need to use array values in
Or better said: When to use array as a field data type in a
I want to take an array and use that array's values to populate an
I need to use a byte array as a profile property in a website.
Is there a way to use Array_unique function working for nested array like below?
Is it okay to use array without single or double quotion like $array[key]? I
If I use an array of linked list to implement a hash table, the
I'm trying to use an array to set the where parameters for a Zend
I'm working on a sparse matrix class that needs to use an array of
In Java, we can always use an array to store object reference. Then we

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.