PHP按日期和字符排序数组

时间:2017-10-17 10:58:03

标签: php arrays

阵列:

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');

预期产出:

[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
    )

执行功能后,我得到了这个数组。现在我想按日期和字符排序数组。我给了我预期的输出任何人都可以请帮助我如何实现我的预期输出?对不起我的语法错误。请编辑此问题以提高可读性,以便其他人可以帮助。

1 个答案:

答案 0 :(得分:0)

以下是使用usort和回调的多排序示例:

<?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);

首先比较日期。只有它们相等才能比较这些字符。

上面会输出:

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"
  }
}

要进行降序排序,您只需翻转比较返回的-11