无法在PHP中按日期对多维数组进行排序

时间:2016-07-30 14:21:37

标签: php arrays sorting

我目前正在尝试在PHP中按截止日期对多维数组进行排序,但排序不成功。我已尝试在这里应用几种解决方案,但没有运气。有人可以就下面代码的正确解决方案提出建议吗?

function getTasksTeam($user) {

    $master = array();
    $master["record"] = array();


    $teamArray = array();
    $teams = mysql_query("SELECT * FROM users WHERE _id = '$user'") or die(mysql_error());
    if (mysql_num_rows($teams) > 0) {
                        while(($row =  mysql_fetch_assoc($teams))) { 
                            $teamArray = explode(':', $row["TEAM"]);

                        }
    }

    for ($i = 0;$i < count($teamArray);$i++) {
        $teamId = $teamArray[$i];

        $query = mysql_query("SELECT * FROM tasks WHERE RESPONSIBLE_TYPE = '0' AND RESPONSIBLE = '$teamId' AND STATUS <> 3") or die(mysql_error());
        if (mysql_num_rows($query) > 0) {
            while (($row = mysql_fetch_assoc($query))) {
                    $record = array();
                    $record["id"] = $row["_id"];
                    $record["task"] = $row["TASK"];
                    $record["part_number"] = $row["PART_NUMBER"];
                    $record["due"] = $row["DATE_DUE"];
                    $record["responsible"] = $row["RESPONSIBLE"];
                    $record["status"] = $row["STATUS"];
                    array_push($master["record"], $record);

            }
        }
    }

    usort($master["record"], 'date_sort');

    $master["success"] = 1;
    header('Content-type: application/json');
    echo json_encode($master);

 }

 function date_sort($a, $b) {
    return strcmp($a['due'], $b['due']); 
 }

1 个答案:

答案 0 :(得分:0)

您可以像这样使用usortstrtotime

usort($master["record"], function($a, $b) {
    return strtotime($a['due']) - strtotime($b['due']);
});

我希望这会对你有所帮助。