【问题标题】:PHP sort array by date and characterPHP按日期和字符排序数组
【发布时间】:2018-10-19 17:07:23
【问题描述】:

数组:

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
    )

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

【问题讨论】:

  • 这些数组是否来自数据库?`如果是,为什么不对查询进行排序?
  • 所以你想 order_by date 和 char ?你是什​​么意思字符?哪个角色?
  • 检查我的预期输出后检查你是否能理解我想要什么。 对试图帮助你的人来说不是很好/建设性的评论。。这里的人都是志愿者
  • 字符...???
  • 哪个角色...@shahrushabh

标签: php arrays


【解决方案1】:

这是一个使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多