Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据两个元素中的日期值对多维数组排序?_Php - Fatal编程技术网

Php 根据两个元素中的日期值对多维数组排序?

Php 根据两个元素中的日期值对多维数组排序?,php,Php,我试图在多维数组中对数组(每个数组包含2个日期值)进行排序。我能够找到一个有用的函数来解决一个元素的问题,但是我无法修改两个元素 目前的问题是对有发布时间和编辑时间的评论进行排序。本质上,我希望将它们从最新到最旧进行排序(同时保留这两个值) 如果这是不可能的,让我知道 编辑:实体模型 $array = array( [0] => array( [0] => "Aug:1:2012 12:00:pm", // post date [1]

我试图在多维数组中对数组(每个数组包含2个日期值)进行排序。我能够找到一个有用的函数来解决一个元素的问题,但是我无法修改两个元素

目前的问题是对有发布时间和编辑时间的评论进行排序。本质上,我希望将它们从最新到最旧进行排序(同时保留这两个值)

如果这是不可能的,让我知道

编辑:实体模型

$array = array(
    [0] => array(
          [0] => "Aug:1:2012 12:00:pm", // post date
          [1] => "Aug:28:2012 12:00:pm"  // edit date
    ),
    [1] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:30:2012 12:00:pm"
    )
    [2] => array(
          [0] => "Aug:29:2012 12:00:pm",
          [1] => "Aug:1:2012 12:00:pm"
    )
};
应首先输出:$array[1](因为它的日期在键1和键2中最高),然后是$array[2],然后是$array[0]

$array = array(
    [0] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:30:2012 12:00:pm" // highest   
    ),
    [1] => array(
          [0] => "Aug:29:2012 12:00:pm", // next
          [1] => "Aug:1:2012 12:00:pm"
    )
    [2] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:28:2012 12:00:pm" // lowest
    )
};

我不确定是否理解正确,但您的意思是希望根据第1列(“发布日期”)对数组进行排序,如果这些值相等,则顺序由第2列决定。因此,您需要的只是修复比较函数:

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    if ($t1 != $t2) {
        return $t1 - $t2;
    }

    // if "post dates" are equal, compare "edit dates"
    $t1 = strtotime($a['datetime2']);
    $t2 = strtotime($b['datetime2']);

    return $t1 - $t2;
}    
编辑:

好的,根据您的评论,您只需要从数组中选取max元素。因此,这应该是可行的:

usort($array, function($a, $b) {
    $t1 = max(strtotime($a[0]), strtotime($a[1]));
    $t2 = max(strtotime($b[0]), strtotime($b[1]));

    return $t1 - $t2;
});

您的排序函数需要首先计算出哪个日期是最近的-发布日期或编辑日期,然后将其用于比较

function sort_arr($arr1, $arr2) {
    $this_posted = strtotime($arr1[0]);
    $this_edited = strtotime($arr1[1]);
    $comparison_posted = strtotime($arr2[0]);
    $comparison_edited = strtotime($arr2[1]);
    $this_date = $this_posted > $this_edited ? $this_posted : $this_edited;
    $comparison_date = $comparison_posted > $comparison_edited ? $comparison_posted : $comparison_edited;
    return $this_date > $comparison_date;
}

$arr = array(
    array("Aug:1:2009 12:00:pm", "Aug:2:2009 12:00:pm"),
    array("Aug:1:2011 12:00:pm", "Jul:21:2012 12:00:pm"),
    array("Aug:5:2011 12:00:pm", "Jan:21:2013 12:00:pm")
);

usort($arr, 'sort_arr');

请出示你的数据样本和你希望结果看起来像什么样的样本。谢谢你的回复,不一定,发布日期不一定相等。把这两个日期元素想象成一个日期池,我所需要的就是将这个池从最新的到最旧的排序。所以两个日期中最大的日期值代表数组。你是什么意思?这不起作用?(这种排序方式与您的示例相反,因此如果您想要完全相同,只需将return改为:return$t2-$t1;)还有一件事:我的示例适用于php>=5.3,因此如果您使用较低版本,则需要使用普通函数,而不是匿名函数。但原则是,目前该系统正在发挥作用。在寻找警告偏移通知的大量工作之后,我注意到动态更正的数组在末尾有一个空数组,从而导致一个没有键的空数组。你的代码非常简单和有用,我非常感谢你!
function sort_arr($arr1, $arr2) {
    $this_posted = strtotime($arr1[0]);
    $this_edited = strtotime($arr1[1]);
    $comparison_posted = strtotime($arr2[0]);
    $comparison_edited = strtotime($arr2[1]);
    $this_date = $this_posted > $this_edited ? $this_posted : $this_edited;
    $comparison_date = $comparison_posted > $comparison_edited ? $comparison_posted : $comparison_edited;
    return $this_date > $comparison_date;
}

$arr = array(
    array("Aug:1:2009 12:00:pm", "Aug:2:2009 12:00:pm"),
    array("Aug:1:2011 12:00:pm", "Jul:21:2012 12:00:pm"),
    array("Aug:5:2011 12:00:pm", "Jan:21:2013 12:00:pm")
);

usort($arr, 'sort_arr');