rolex rolex - 1 month ago 10
PHP Question

JSON Sorting by date

First of all i'm a complete newbie to this json, i have searched for solutions but seem i did not find the trick.

Where data looks like this:

Array
(
[0] => Array
(
[debtor_name] => Name 1
[debtor_number] => 0001
[statement_date] => 7/31/17
[statement_period] => 07/01/2017 - 07/31/2017
[due_date] => 8/31/17
[statement_number] => 2017-07-0001
[courier] => Handcarry
[outstanding_bal] => 3,647.53
[bal_br_fwd] => 3,647.16
[age_cur] => 3,647.53
[age_30] => 0
[age_60] => 0
[age_90] => 0
[age_120] => 0
[password] => 11291934
[email] => rolex@bngtransmedia.com
[counter] => 1
[transList] => Array
(
[0] => Array
(
[lineOrder] => 0
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000484236
[amt] => 7231.16
[desc] => MD-MONTHLY DUES
[debit] => 3584
[credit] => 0
)

[1] => Array
(
[lineOrder] => 1
[date] => 7/15/17
[dateNum] => 1500048000000
[ref] => 0000165008
[amt] => 3684
[desc] => CM-METROBANK
[debit] => 0
[credit] => 3647.16
)

[2] => Array
(
[lineOrder] => 2
[date] => 7/15/17
[dateNum] => 1500048000000
[ref] => 0000165008
[amt] => 3647.53
[desc] => 1% Discount
[debit] => 0
[credit] => 36.47
)

[3] => Array
(
[lineOrder] => 3
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000484237
[amt] => 7331.16
[desc] => MD-VALLEY SENIORS FEE
[debit] => 100
[credit] => 0
)

)

)

[1] => Array
(
[debtor_name] => Name 2
[debtor_number] => 2665
[statement_date] => 7/31/17
[statement_period] => 07/01/2017 - 07/31/2017
[due_date] => 8/31/17
[statement_number] => 2017-07-2665
[courier] => Sure Mail
[outstanding_bal] => 18,194.93
[bal_br_fwd] => 17,897.63
[age_cur] => 5,193.00
[age_30] => 6,196.01
[age_60] => 4,984.71
[age_90] => 1,821.21
[age_120] => 0
[password] => 05291962
[email] => r.estemonio@gmail.com
[counter] => 5372
[transList] => Array
(
[0] => Array
(
[lineOrder] => 4
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000479082
[amt] => 18082.63
[desc] => F&B CONCESSIONAIRE-F&B 015904 JJ
[debit] => 185
[credit] => 0
)

[1] => Array
(
[lineOrder] => 5
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000485742
[amt] => 21666.63
[desc] => MD-MONTHLY DUES
[debit] => 3584
[credit] => 0
)

[2] => Array
(
[lineOrder] => 6
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000485743
[amt] => 21890.63
[desc] => MD-LOCKER RENTAL
[debit] => 224
[credit] => 0
)

[3] => Array
(
[lineOrder] => 7
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000481000
[amt] => 21920.63
[desc] => F&B CONCESSIONAIRE-F&B 026492 ANIX
[debit] => 30
[credit] => 0
)

[4] => Array
(
[lineOrder] => 8
[date] => 7/31/17
[dateNum] => 1501430400000
[ref] => PF00072305
[amt] => 18194.93
[desc] => PATRONAGE FEE
[debit] => 0
[credit] => 448
)

[5] => Array
(
[lineOrder] => 9
[date] => 7/6/17
[dateNum] => 1499270400000
[ref] => 0000481002
[amt] => 22010.63
[desc] => F&B CONCESSIONAIRE-F&B 027375 ANIX
[debit] => 15
[credit] => 0
)

[6] => Array
(
[lineOrder] => 10
[date] => 7/8/17
[dateNum] => 1499443200000
[ref] => 0000479083
[amt] => 22268.63
[desc] => F&B CONCESSIONAIRE-F&B 016332 JJ
[debit] => 258
[credit] => 0
)

[7] => Array
(
[lineOrder] => 11
[date] => 7/8/17
[dateNum] => 1499443200000
[ref] => 0000164358
[amt] => 17372.93
[desc] => CM-METROBANK
[debit] => 0
[credit] => 4895.7
)

[8] => Array
(
[lineOrder] => 12
[date] => 7/15/17
[dateNum] => 1500048000000
[ref] => 0000481003
[amt] => 17422.93
[desc] => F&B CONCESSIONAIRE-F&B 028985 ANIX
[debit] => 50
[credit] => 0
)

[9] => Array
(
[lineOrder] => 13
[date] => 7/15/17
[dateNum] => 1500048000000
[ref] => 0000479084
[amt] => 17735.93
[desc] => F&B CONCESSIONAIRE-F&B 016716 JJ
[debit] => 313
[credit] => 0
)

[10] => Array
(
[lineOrder] => 14
[date] => 7/22/17
[dateNum] => 1500652800000
[ref] => 0000478834
[amt] => 18289.93
[desc] => F&B CONCESSIONAIRE-08528DR
[debit] => 554
[credit] => 0
)

[11] => Array
(
[lineOrder] => 15
[date] => 7/29/17
[dateNum] => 1501257600000
[ref] => 0000479085
[amt] => 18642.93
[desc] => F&B CONCESSIONAIRE-F&B 017434 JJ
[debit] => 353
[credit] => 0
)

[12] => Array
(
[lineOrder] => 16
[date] => 7/1/17
[dateNum] => 1498838400000
[ref] => 0000481001
[amt] => 21995.63
[desc] => F&B CONCESSIONAIRE-F&B 026713 ANIX
[debit] => 75
[credit] => 0
)

)

)

)


So i'm getting the data like this

$json = json_decode($_POST["list"], true);


I tried this line of code but seems not working:

function sort_by_date($a, $b) {
$a = strtotime($a['date']);
$b = strtotime($b['date']);
return ($a < $b) ? -1 : 1;
}
usort($json, 'sort_by_date');


I am trying to sort this JSON array by date in transList Array but i don't really know how i should call this variable on the array, any tips in the right direction are much appreciated!

Answer Source

This sorts the arrays in each transList:

foreach( $json as $k => $v )
{
    usort( $v['transList'], function( $a, $b ){
        $d1 = new DateTime( $a['date'] );
        $d2 = new DateTime( $b['date'] );
        if( $d1 == $d2 ){
            return 0;
        }
        return ($d1 < $d2) ? -1 : 1;
    });

    // For testing purposes, the array is print to screen to show sorting worked as required
    echo '<pre>';
    print_r( $v['transList'] );
    echo '</pre>';
}

And just for testing purposes, I used the following data to mock your data:

$json = [
    [
        'transList' => [
            [
                'id'   => 'a',
                'date' => '7/15/17'
            ],
            [
                'id'   => 'b',
                'date' => '2/15/17'
            ],
            [
                'id'   => 'c',
                'date' => '7/15/16'
            ],
            [
                'id'   => 'd',
                'date' => '4/15/12'
            ],
            [
                'id'   => 'e',
                'date' => '3/15/16'
            ]
        ]
    ],
    [
        'transList' => [
            [
                'id'   => 'd',
                'date' => '7/15/17'
            ],
            [
                'id'   => 'b',
                'date' => '2/15/17'
            ],
            [
                'id'   => 'g',
                'date' => '7/15/16'
            ],
            [
                'id'   => 'a',
                'date' => '4/15/12'
            ],
            [
                'id'   => 'x',
                'date' => '3/15/16'
            ]
        ]
    ]
];

To answer your question about further sorting by amount if date is equal:

foreach( $json as $k => $v )
{
    usort( $v['transList'], function( $a, $b ){
        $d1 = new DateTime( $a['date'] );
        $d2 = new DateTime( $b['date'] );
        if( $d1 == $d2 ){
            if( $a['amt'] == $b['amt'] ){
                return 0;
            }
            return ($a['amt'] < $b['amt']) ? -1 : 1;
        }
        return ($d1 < $d2) ? -1 : 1;
    });

    // For testing purposes, the array is print to screen to show sorting worked as required
    echo '<pre>';
    print_r( $v['transList'] );
    echo '</pre>';
}