我有一个包含多个具有许多属性的对象的数组。
我想根据两个对象属性在 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属性,按照最新的date_sent排序。请注意,它不是基于谁拥有更高的 not_read 属性。
然后那些 not_read 属性为 0 的将按最新的 date_sent 排序。
谁能帮我完成这个程序?
非常感谢您的关注!