Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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_Sorting - Fatal编程技术网

如何在PHP中对具有许多属性的对象数组进行排序?

如何在PHP中对具有许多属性的对象数组进行排序?,php,sorting,Php,Sorting,我有一个数组,它包含多个具有许多属性的对象 我想在PHP中根据两个对象属性对其进行排序 下面是一组对象示例,让您了解我正在处理的数据: Array ( [0] => stdClass Object ( [username] => user98 [sender_id] => 98 [date_sent] => 2012-07-25 00:52:11 [not_read] => 0 )

我有一个数组,它包含多个具有许多属性的对象

我想在PHP中根据两个对象属性对其进行排序

下面是一组对象示例,让您了解我正在处理的数据:

Array (
    [0] => stdClass Object (
        [username] => user98
        [sender_id] => 98
        [date_sent] => 2012-07-25 00:52:11
        [not_read] => 0
    )
    [1] => stdClass Object (
        [username] => user87
        [sender_id] => 87
        [date_sent] => 2012-07-25 00:59:15
        [not_read] => 1
    )
    [2] => stdClass Object (
        [username] => user93
        [sender_id] => 93
        [date_sent] => 2012-07-25 00:52:13
        [not_read] => 2
    )
    [3] => stdClass Object (
        [username] => user5
        [sender_id] => 5
        [date_sent] => 2012-07-25 00:52:16
        [not_read] => 0
    )
)
我需要对其进行排序,以生成此数组:

Array (
    [1] => stdClass Object (
        [username] => user87
        [sender_id] => 87
        [date_sent] => 2012-07-25 00:59:15
        [not_read] => 1
    )
    [2] => stdClass Object (
        [username] => user93
        [sender_id] => 93
        [date_sent] => 2012-07-25 00:52:13
        [not_read] => 2
    )
    [3] => stdClass Object (
        [username] => user5
        [sender_id] => 5
        [date_sent] => 2012-07-25 00:52:16
        [not_read] => 0
    )

    [0] => stdClass Object (
        [username] => user98
        [sender_id] => 98
        [date_sent] => 2012-07-25 00:52:11
        [not_read] => 0
    )


)
排序基于对象的date属性和not_read属性,not_read>0在排序中优先,然后它将查看date_sent属性,并根据最近发送的日期对其排序。请注意,它不是基于谁具有较高的not_read属性

然后,那些具有0 not_read属性的将按最新发送日期进行排序

有人能帮我做这个项目吗


非常感谢您的关注

您需要使用用户定义的排序函数:

function sortByDate($a, $b)
{
    if($a->not_read > $b->not_read)
        return 1;
    if($a->not_read < $b->not_read)
        return -1;
    if(strtotime($a->date_sent) > strtotime($b->date_sent))
        return 1;
    if(strtotime($a->date_sent) < strtotime($b->date_sent))
        return -1;
    return 0;
}
传入的数组现在将被排序。

函数sortByDate($a,$b)
function sortByDate($a, $b)
{
    if($a->not_read > 0 && $b->not_read == 0)
        return -1;
    if($b->not_read > 0 && $a->not_read == 0)
        return 1;
    if ($a->not_read == 0 && $b->not_read == 0 || $a->not_read > 0 && $b->not_read > 0){
        if(strtotime($a->date_sent) > strtotime($b->date_sent))
            return -1;
        if(strtotime($a->date_sent) < strtotime($b->date_sent))
            return 1;
    }

    return 0;
}

usort($array_to_sort, 'sortByDate');
{ 如果($a->not_-read>0&&$b->not_-read==0) 返回-1; 如果($b->not_-read>0&&$a->not_-read==0) 返回1; 如果($a->not_read==0&&$b->not_read==0 | |$a->not_read>0&&$b->not_read>0){ if(strotime($a->date\u sent)>strotime($b->date\u sent)) 返回-1; if(strotime($a->date\u sent)date\u sent)) 返回1; } 返回0; } usort($array_to_sort,'sortByDate');

注:我当然会对Patrick's进行编辑,但我不确定我的是否有效。他在正确的轨道上。

我被这段代码弄糊涂了,我的意思是如果$a->not_read中的if语句已经返回,那么代码将如何到达strotime?@KevinLee因为您的第一个排序标准是
not_read
属性,所以
发送日期不重要。只有当
not_read
属性相同时,您才需要担心
date\u sent
属性。@MatthewScragg排序函数中的
not_read
属性的条件不正确。仅当两个比较对象的
not\u read
属性为>0且另一个对象的
not\u read
属性为0时,才进行检查。在OP给出的示例中,为
not\u read
给出了三个值:0,1,2。排序函数无法正确排序这些对象。@Patrick再次阅读操作,似乎他不在乎not_Read是什么,只要not_Read>0在前面,并且仍然按日期排序。@MatthewScragg啊,错过了那部分。
function sortByDate($a, $b)
{
    if($a->not_read > 0 && $b->not_read == 0)
        return -1;
    if($b->not_read > 0 && $a->not_read == 0)
        return 1;
    if ($a->not_read == 0 && $b->not_read == 0 || $a->not_read > 0 && $b->not_read > 0){
        if(strtotime($a->date_sent) > strtotime($b->date_sent))
            return -1;
        if(strtotime($a->date_sent) < strtotime($b->date_sent))
            return 1;
    }

    return 0;
}

usort($array_to_sort, 'sortByDate');