-5

Array:

Array
(
    [0] => Array
        (
            [Create_date] => 2017-10-17
            [Description] => Cash
            [Debit] => 
            [Credit] => 27612
            [Type] => Credit
        )

    [1] => Array
        (
            [Create_date] => 2017-10-17
            [Description] => Invoice ID = 22
            [Debit] => 27612
            [Credit] => 
            [Type] => Debit
        )

    [2] => Array
        (
            [Create_date] => 2017-10-17
            [Description] => Invoice ID = 20
            [Debit] => 1008
            [Credit] => 
            [Type] => Debit
        )

    [3] => Array
        (
            [Create_date] => 2017-10-17
            [Description] => Invoice ID = 19
            [Debit] => 1168.2
            [Credit] => 
            [Type] => Debit
        )

    [4] => Array
        (
            [Create_date] => 2017-10-17
            [Description] => Invoice ID = 18
            [Debit] => 276.12
            [Credit] => 
            [Type] => Debit
        )

)

PHP:

function date_compare($a, $b)
{
    $t1 = strtotime($a['Create_date']);
    $t2 = strtotime($b['Create_date']);
    return $t1 - $t2;
}
usort($data['Ledgers'], 'date_compare');

Expected Output:

[0] => Array
    (
        [Create_date] => 2017-10-17
        [Description] => Invoice ID = 22
        [Debit] => 27612
        [Credit] => 
        [Type] => Debit
    )

[1] => Array
    (
        [Create_date] => 2017-10-17
        [Description] => Invoice ID = 20
        [Debit] => 1008
        [Credit] => 
        [Type] => Debit
    )

[2] => Array
    (
        [Create_date] => 2017-10-17
        [Description] => Invoice ID = 19
        [Debit] => 1168.2
        [Credit] => 
        [Type] => Debit
    )

[3] => Array
    (
        [Create_date] => 2017-10-17
        [Description] => Invoice ID = 18
        [Debit] => 276.12
        [Credit] => 
        [Type] => Debit
    )
[4] => Array
    (
        [Create_date] => 2017-10-17
        [Description] => Cash
        [Debit] => 
        [Credit] => 27612
        [Type] => Credit
    )

After executing function I got this array. Now I want to order array by date and character. I gave my expected output anyone can please Help me How can I achieve my expected output? Sorry for my grammatical mistakes. Please edit this question for readability so it may help others.

Alessandro
  • 900
  • 12
  • 23

1 Answers1

0

Here's an example multi-sort using usort and a callback:

<?php
$arr = [
    ['date' => '2017-10-18', 'char' => 'Z'],
    ['date' => '2017-10-17', 'char' => 'Z'],
    ['date' => '2017-9-2', 'char' => 'A'],
    ['date' => '2017-10-17', 'char' => 'A'],
    ['date' => '2017-10-18', 'char' => 'A'],
];

usort($arr, function($a, $b) {
    $date_a = strtotime($a['date']);
    $date_b = strtotime($b['date']);

    if($date_a < $date_b)
        return -1;

    if($date_a > $date_b)
        return 1;

    $char_a = $a['char'];
    $char_b = $b['char'];

    if($char_a == $char_b)
        return 0;

    return $char_a < $char_b ? -1 : 1;
});

var_dump($arr);

First you compare the dates. Only if they are equal do you compare the chars.

The above would output:

array(5) {
  [0] =>
  array(2) {
    'date' =>
    string(8) "2017-9-2"
    'char' =>
    string(1) "A"
  }
  [1] =>
  array(2) {
    'date' =>
    string(10) "2017-10-17"
    'char' =>
    string(1) "A"
  }
  [2] =>
  array(2) {
    'date' =>
    string(10) "2017-10-17"
    'char' =>
    string(1) "Z"
  }
  [3] =>
  array(2) {
    'date' =>
    string(10) "2017-10-18"
    'char' =>
    string(1) "A"
  }
  [4] =>
  array(2) {
    'date' =>
    string(10) "2017-10-18"
    'char' =>
    string(1) "Z"
  }
}

To do a descending sort you'd just flip the -1 and 1 being returned by the comparison.

Jeff Standen
  • 6,670
  • 1
  • 17
  • 18