如何在PHP中使用带有特定键的数组时按ASC顺序进行排序

时间:2018-11-29 05:19:57

标签: php arrays

这里我有一个数组,我正在使用foreach并编写条件是否一切正常,我的问题是,否则条件array_push($priorityList4, $temp),我正在这样做

activationDate按照ASC顺序,我必须将数据推送到$priorityList4

  

我的代码

<?php
$priorityList4 = $priorityList0 = [];
$pedagogyTopics = Array
        (
            "0" => Array
                (
                    "pedagogyID" => "1",
                    "status" => "complete",
                    "activationDate" => "2018-11-28 16:21:06"
                ),

            "1" => Array
                (
                    "pedagogyID" => "2",
                    "status" => "in-progress",
                    "activationDate" => "2018-11-28 15:19:22"
                ),
            "2" => Array
                (
                    "pedagogyID" => "3",
                    "status" => "in-progress",
                    "activationDate" => "2018-11-28 14:19:22"
                )

        );
foreach ($pedagogyTopics as $topic) {
    $temp = [
                "contentID" => $topic['pedagogyID'],
                "status" => isset($topic['status']) ? $topic['status'] : null,
                "activationDate" => $topic['activationDate'],
            ];

            if($topic['status'] != 'complete'){
                 array_push($priorityList0, $temp);
            }else{
                 array_push($priorityList4, $temp); // Here activationDate ASC order wise i have to push
            }

}

echo '<pre>';
print_r(array_merge($priorityList0 , $priorityList4));
echo '</pre>';
?>
  

获取输出

Array
(
    [0] => Array
        (
            [contentID] => 2
            [status] => in-progress
            [activationDate] => 2018-11-28 15:19:22
        )

    [1] => Array
        (
            [contentID] => 3
            [status] => in-progress
            [activationDate] => 2018-11-28 14:19:22
        )

    [2] => Array
        (
            [contentID] => 1
            [status] => complete
            [activationDate] => 2018-11-28 16:21:06
        )

)
  

预期产量

Array
(
    [0] => Array
        (
            [contentID] => 3
            [status] => in-progress
            [activationDate] => 2018-11-28 14:19:22
        )

    [1] => Array
        (
            [contentID] => 2
            [status] => in-progress
            [activationDate] => 2018-11-28 15:19:22
        )

    [2] => Array
        (
            [contentID] => 1
            [status] => complete
            [activationDate] => 2018-11-28 16:21:06
        )

)

0 个答案:

没有答案