Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

PHP custom sort function of multidimensional array because of usort members equality problem

I have a problem sorting some arrays because some comparing values are the same and usort can't be controlled in which order those should be returned. This is a limitation of usort which I can't overcome unless I use some custom sorting function. This is where I need the help.

I have this array:

Array
(
    [0] => Array
        (
            [date] => 23/01/2021
            [time] => 15:25
            [name] => name1
        )

    [1] => Array
        (
            [date] => 23/01/2021
            [time] => 15:27
            [name] => name2
        )

    [2] => Array
        (
            [date] => 26/01/2021
            [time] => 07:19
            [name] => name3
        )

    [3] => Array
        (
            [date] => 26/01/2021
            [time] => 07:24
            [name] => name4
        )

    [4] => Array
        (
            [date] => 27/01/2021
            [time] => 08:38
            [name] => name5
        )

    [5] => Array
        (
            [date] => 27/01/2021
            [time] => 08:38
            [name] => name6
        )

    [6] => Array
        (
            [date] => 27/01/2021
            [time] => 10:09
            [name] => name7
        )

    [7] => Array
        (
            [date] => 27/01/2021
            [time] => 16:40
            [name] => name8
        )
)

I want to sort the array in chronologial order (newest to oldest) but as you can see name5 and name6 have the same date and time. I want these to be returned in the order they appear (the smallest key first).

What I would like as an outcome for example in this specific array is a reverse order (since they are in oldest to newest) but name5 and name6 not to be reversed (because I want to keep the order they are in).

How can I do this? Not specifically for this table, but maybe on another table where 3 items have the same date/time.

Thanks

question from:https://stackoverflow.com/questions/65927681/php-custom-sort-function-of-multidimensional-array-because-of-usort-members-equa

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
foreach( $arr as $i => &$x ){ $x['k'] = $i; }

This adds a column to contain the key you're wanting to sort by. Then, just do your usort operation, that falls back to the key compare if date/time is equal. I added a simple helper function to fix your dates so strtotime can parse it.

function o_to_time( $o ){
    return strtotime( implode( "/" , array_reverse( explode( "/" , $o["date"] ) ) ) . " " . $o["time"] );
}

usort( $arr , function( $a , $b ){
    return o_to_time( $b ) <=> o_to_time( $a ) ?: $a['k'] <=> $b['k'];
} );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...