在PHP中按日期排序数组

问题描述 投票:0回答:1

我试着按日期对array进行排序,但结果似乎没有了。正如你在下面的图片中看到的那样,0 index应该在最后。

结果:

enter image description here

我的守则

$next_Data = json_decode('[{"EmployeeID":102,"DateTimeRecord":"7/31/2018 9:39:13 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"7/31/2018 9:39:13 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"7/31/2018 9:39:13 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:43:04 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:43:05 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:43:06 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:13:25 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:13:25 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 20:13:25 PM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:13:25 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 6:54:12 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 6:54:12 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 6:54:12 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 12:13:25 PM","InOut":"0"}]');


$next_Data = array_map("unserialize", array_unique(array_map("serialize", $next_Data)));

        foreach ($next_Data as $key => $part) 
        {
            $sort[$key] = strtotime($part->DateTimeRecord);
        }

array_multisort($sort, SORT_ASC, $next_Data);
php arrays sorting
1个回答
1
投票

strtotime()为此值返回FALSE,因此它被排序为0,因此首先出现。 20:13 PM不是有效时间;它应该是8:13 PM20:13

© www.soinside.com 2019 - 2024. All rights reserved.