1

PHP Code:

$data['Ledgers'] = array_merge($data['Invoices'],$data['Payments']);
usort($data['Ledgers'], function( $a, $b ) {return strtotime($a["Create_date"]) - strtotime($b["Create_date"]);});

Output:

[Ledgers] => Array
        (
            [0] => Array
                (
                    [Title] => Cash
                    [Create_date] => 2017-09-14
                    [Amount] => 12000
                    [Type] => Credit
                )

            [1] => Array
                (
                    [Title] => 24
                    [Create_date] => 2017-09-14
                    [Amount] => 12600
                    [Type] => Debit
                )
            [2] => Array
                (
                    [Title] => 25
                    [Create_date] => 2017-09-14
                    [Amount] => 1000
                    [Type] => Debit
                )

        )
[Payments] => Array
    (
        [0] => Array
            (
                [Title] => Cash
                [Create_date] => 2017-09-14
                [Amount] => 12000
                [Type] => Credit
            )

    )

[Invoices] => Array
    (
        [0] => Array
            (
                [Title] => 24
                [Create_date] => 2017-09-14
                [Amount] => 12600
                [Type] => Debit
            )

    )

Expected Output:

[Ledgers] => Array
        (

            [0] => Array
                (
                    [Title] => 24
                    [Create_date] => 2017-09-14
                    [Amount] => 12600
                    [Type] => Debit
                )
            [1] => Array
                (
                    [Title] => 25
                    [Create_date] => 2017-09-14
                    [Amount] => 1000
                    [Type] => Debit
                )
            [2] => Array
                (
                    [Title] => Cash
                    [Create_date] => 2017-09-14
                    [Amount] => 12000
                    [Type] => Credit
                )

        )

Anyone can help me How can I sort two keys 1st is Create_date = ASC and 2nd is Type = DESC? sorry for my weak English. please try to improve readability of this question if you can. your help is appreciated.

Someone requested for Payments and Invoices. I posted both arrays. now please help me.

1 Answers1

1

Something like this should work:

usort(
    $data['Ledgers'], 
    function( $a, $b ) {
        $time_diff = strtotime($a["Create_date"]) - strtotime($b["Create_date"]);

        // if time differernce is zero - return comparison of `Type` 
        // fields multiplied by `-1` to sort in descending order
        return $time_diff === 0? -1 * strcmp($a['Type'], $b['Type']) : $time_diff;

        // or notice comparing `$b` first, here you don't need `-1`
        return $time_diff === 0? strcmp($b['Type'], $a['Type']) : $time_diff;
    }
);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • because the `Create_date` is in YYYY-MM-DD format, a simple string comparison should be sufficient to sort it – Wee Zel Sep 14 '17 at 13:45