如何在Laravel PHP中按日期对JSON数组进行排序

时间:2018-09-06 22:55:55

标签: php json laravel sorting

嗨,我正在使用PHP laravel生成JSON响应。这是我的函数,它将生成此响应

public function getReport($employeeCode){
    $postData = Input::all();

    $companyName = $postData['company_name'];
    $date = $postData['date'];
    $month = $postData['month'];
    $year = $postData['year'];
    $visitReason = $postData['visit_reason'];

    (database query applied here which gets data in $report)

    $countReportSize = count($report);
    $allDatas = [];
        for($i = 0; $i < $countReport; $i++)
        {
            $date = $report[$i]->day;
            $month = $report[$i]->month;
            $year = $report[$i]->year;
            $desc = $report[$i]->description;

            $countDateSize = explode(',',$date);
            $countMonth = explode(',',$month);
            $countYear = explode(',',$year);
            $countDescription = explode(',',$desc);

            for($j= 0; $j < count($countDateSize); $j++)
            {
                $data = [
                    'inquiry_id' => $report[$i]->inquiry_id,
                    'date' => $countDateSize[$j].'-'.$countMonth[$j].'-'.$countYear[$j],

                ];
                array_push($allDatas, $data);
            }
        }
        return response()->json($allDatas);
}

现在此函数将在下面的JSON响应中生成给定,问题是如何按日期对它进行排序?

[
{
"inquiry_id": "2",
"date": "18-03-2018"
},
{
"inquiry_id": "2",
"date": "15-03-2018"
}
]

我需要按以下日期对响应进行排序:

[
{
"inquiry_id": "2",
"date": "15-03-2018"
},

{
"inquiry_id": "2",
"date": "18-03-2018"
}
]

任何帮助将不胜感激。 预先感谢。

1 个答案:

答案 0 :(得分:0)

使用$ myArray = json_decode($ json_info,true);将JSON转换为数组;

然后使用

$date = array_column($myArray, 'date');
        array_multisort($date, SORT_ASC, $myArray);

现在$ myArray将按日期升序进行排序,