如何按日期字符串对$ _POST数组进行排序

时间:2017-05-31 09:51:57

标签: php arrays

我在提交表单时传递了一个POST数组。我遍历它并使用以下结果将结果放入表中:

<table id="myTable">
<thead>
    <tr><td>Exam Date</td><td>Subject Name</td><td>Subject Code</td><td>AM/PM</td></tr>
</thead>
<tbody>
<?php

$rowCount =  count($_POST['exam_code_input']);
//var_dump($_POST); 
    for($i=0; $i<$rowCount; $i++)
        {
        echo "<tr>";
        echo "<td>".$_POST['exam_date_input'][$i]."</td>";
        echo "<td>".$_POST['exam_code_input'][$i]."</td>";
        echo "<td>".$_POST['exam_title'][$i]."</td>";
        echo "<td>".$_POST['exam_time_input'][$i]."</td>";
        echo "</tr>";
        }
?>
</tbody>
</table> 

我需要按日期对表行进行排序。目前,它通过索引将条目放入表中。这是var_dump($_POST)

array(13) { 
    ["your_name"]=> string(8) "Ian TEST" 
    ["candidate_number"]=> string(5) "12345" 
    ["centre_number"]=> string(4) "TEST" 
    ["school_year"]=> string(2) "12" 
    ["email"]=> string(23) "ianbutler82@yahoo.co.uk" 
    ["mobile_number"]=> string(11) "12345455676" 
    ["exam_board"]=> string(2) "-1" 
    ["exam_date_input"]=> array(7) { 
            [0]=> string(10) "05/16/2017" 
            [1]=> string(10) "05/08/2017" 
            [2]=> string(10) "05/12/2017" 
            [3]=> string(10) "05/25/2017" 
            [4]=> string(10) "06/06/2017" 
            [5]=> string(10) "06/12/2017" 
            [6]=> string(10) "06/20/2017" 
            } 
    ["exam_code_input"]=> array(7) { 
            [0]=> string(9) "Arabic AL" 
            [1]=> string(10) "Biology AL" 
            [2]=> string(10) "Biology AL" 
            [3]=> string(10) "Biology AL" 
            [4]=> string(10) "Biology AL" 
            [5]=> string(10) "Biology AL" 
            [6]=> string(10) "Biology AL" 
            } 
    ["exam_title"]=> array(7) { 
            [0]=> string(34) "Understanding and Written Response" 
            [1]=> string(14) "Biology Unit 3" 
            [2]=> string(14) "Biology Unit 6" 
            [3]=> string(14) "Biology Unit 1" 
            [4]=> string(14) "Biology Unit 2" 
            [5]=> string(14) "Biology Unit 4" 
            [6]=> string(14) "Biology Unit 5" 
            } 
    ["exam_time_input"]=> array(7) { 
            [0]=> string(2) "AM" 
            [1]=> string(2) "PM" 
            [2]=> string(2) "PM" 
            [3]=> string(2) "PM" 
            [4]=> string(2) "PM" 
            [5]=> string(2) "PM" 
            [6]=> string(2) "AM" 
            } 
    ["token"]=> string(32) "bd62bdf4ba8a00174281499decb94d87" 
    ["btSubmit"]=> string(18) "Generate Timetable" 
}

3 个答案:

答案 0 :(得分:1)

所以你要排序&#34; exam_date_input&#34; -Array,对吧?它确实与其他数据相对应,例如&#34; exam_code_input&#34;?

使用DateTime :: createFromFormat,您可以首先从日期值创建一个DateTime对象(例如05/08/2017),然后从中获取Timestamp(getTimestamp())。 然后我会建议你像你已经做的那样迭代你的POST数据并创建一个新数组以获得更好的数据结构。 像

array( "{THETIMESTAMP}" => {all the other corresponding data, like in exam_code_input} )

使用kso​​rt(),您可以按升序键对数组进行排序。

答案 1 :(得分:0)

您需要使用自己的排序功能。

function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
    $reference_array = array();

    foreach($array as $key => $row) {
        $reference_array[$key] = $row[$column];
    }

    array_multisort($reference_array, $direction, $array);
}

呼叫就像:

array_sort_by_column($events_array, 'date');

var_dump($events_array);

答案 2 :(得分:0)

你可以使用usort来实现这个

usort($myArray, function($a,$b){
   return ((new DateTime($a['DatesColumn'])) < (new DateTime($b['DatesColumn']))) ? -1 : 1;
});