按最近日期对php对象进行排序

时间:2020-08-01 06:43:17

标签: php arrays wordpress sorting object

我有通过新的WP_Query($ query_args)获取的php对象。我需要按最近的日期对该对象进行排序。每个事件都有开始日期和结束日期。如果活动的开始日期接近今天,则该活动应该是输出中的第一个事件。

这是我的$ result值对象。

List<Student> student = new List<Student>();
foreach(var item in studentCollection)
{
    var std = new Student()
    {
        Id = int.Parse(item[0]),
        FirstName = item[1],
        LastName = item[2],
        Age = int.Parse(item[3])
    };
    student.Add(std);
}

我以这种方式解决了我的问题。

            array(4) {
          [0]=>
          object(WP_Post)#3813 (24) {
            ["ID"]=>
            int(9905)
            ["post_author"]=>
            string(2) "31"
            ["post_date"]=>
            string(19) "2020-07-30 21:14:06"
            ["post_date_gmt"]=>
            string(19) "2020-07-30 21:14:06"
            ["post_content"]=>
            string(251) "ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad"
            ["post_title"]=>
            string(20) "Test 3 same location"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(20) "test-3-same-location"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-30 21:15:32"
            ["post_modified_gmt"]=>
            string(19) "2020-07-30 21:15:32"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(62) "https://www.stag.wediscover.club/listing/test-3-same-location/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
          [1]=>
          object(WP_Post)#3812 (24) {
            ["ID"]=>
            int(9903)
            ["post_author"]=>
            string(2) "31"
            ["post_date"]=>
            string(19) "2020-07-29 20:32:40"
            ["post_date_gmt"]=>
            string(19) "2020-07-29 20:32:40"
            ["post_content"]=>
            string(167) "Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events"
            ["post_title"]=>
            string(10) "Location 2"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(10) "location-2"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-30 20:53:08"
            ["post_modified_gmt"]=>
            string(19) "2020-07-30 20:53:08"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(52) "https://www.stag.wediscover.club/listing/location-2/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
          [2]=>
          object(WP_Post)#3816 (24) {
            ["ID"]=>
            int(9901)
            ["post_author"]=>
            string(2) "31"
            ["post_date"]=>
            string(19) "2020-07-29 20:31:20"
            ["post_date_gmt"]=>
            string(19) "2020-07-29 20:31:20"
            ["post_content"]=>
            string(158) "Location test 1 Location test 1 Location test 1 Location test 1 Location test 1 Location test 1Location test 1 Location test 1 Location test 1 Location test 1"
            ["post_title"]=>
            string(15) "Location test 1"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(15) "location-test-1"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-30 20:20:56"
            ["post_modified_gmt"]=>
            string(19) "2020-07-30 20:20:56"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(57) "https://www.stag.wediscover.club/listing/location-test-1/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
          [3]=>
          object(WP_Post)#3817 (24) {
            ["ID"]=>
            int(9747)
            ["post_author"]=>
            string(2) "29"
            ["post_date"]=>
            string(19) "2020-07-02 21:24:09"
            ["post_date_gmt"]=>
            string(19) "2020-07-02 21:24:09"
            ["post_content"]=>
            string(525) "Join our most popular surfing meetup in Los Angeles.

        Surfers with all experience levels welcome!

        Approximate schedule for the morning:

        8.45 – 9.00 Meeting next to Perry’s Cafe

        9.00 – 9.30 Surfing

        9.30 – 9.45 Coffe / Snacks

        9.45 – 10.15 Surfing

        10.15 – 10.30 Coffe/ Snacks

        10.30 – 11.00 Volleyball

        We advise you to bring hot coffee or tea with you to make this morning even more cozier after surfing)

        Feel free to contact us if you have any questions.

        We can’t wait to see you!"
            ["post_title"]=>
            string(40) "Surfing and Volleyball at Torrance beach"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(42) "surfing-and-volleyball-at-torrance-beach-4"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-15 23:06:59"
            ["post_modified_gmt"]=>
            string(19) "2020-07-15 23:06:59"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(84) "https://www.stag.wediscover.club/listing/surfing-and-volleyball-at-torrance-beach-3/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
        }

但我确定有更好的解决方案。我想改善这种排序。

对不起,我的英语:)

谢谢。

0 个答案:

没有答案