基于开始和结束值的PHP排序数组

时间:2016-04-20 08:52:55

标签: php arrays loops sorting

我有一个包含不同事件的数组。每个活动都有一个开始时间和结束时间。我想根据这些值对数组进行排序。

Array
(
    [41] => Array
        (
            [0] => Array
                (
                    [event] => event1
                    [start] => 0930
                    [end] => 1200
                )

            [1] => Array
                (
                    [event] => event1
                    [start] => 1000
                    [end] => 1230
                )

            [2] => Array
                (
                    [event] => event1
                    [start] => 1100
                    [end] => 1300
                )

            [3] => Array
                (
                    [event] => event1
                    [start] => 1400
                    [end] => 1630
                )

        )

    [52] => Array
        (
            [0] => Array
                (
                    [event] => event2
                    [start] => 0930
                    [end] => 1030
                )

            [1] => Array
                (
                    [event] => event2
                    [start] => 0930
                    [end] => 1030
                )

        )
)

这就是我的循环以及我尝试对其进行排序的内容:

foreach($attractieIDs as $id) {
        foreach($slots[$id] as $attractions){

            $sortedSlots = usort($slots[$id], function($a, $b){
                return $a['start'] - $b['start'];
            });

        }
    }

对你们说实话,我实际上不知道我在做什么:D

即使我已经多次通过php手册阅读,我也不太了解usort的工作原理

任何可以帮助我的人

提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以在这里使用usort()$myArray是您当前的数组。

    $myArray= Array( 41=>array( 
        0=>array(
            "event" => "event1",
            "start" => 930,
            "end" => 1200
        ),
        1=>array(
            "event" => "event3",
            "start" => 1100,
            "end" => 1300
        ),
        2=>array(
            "event" => "event2",
            "start" => 1000,
            "end" => 1230
        )
    ),
    52=> array(
        0=>array(
            "event" => "event1",
            "start" => 930,
            "end" => 1030
        ),
        1=>array(
            "event" => "event3",
            "start" => 1100,
            "end" => 1300
        ),
    )
);

<强>代码

function sortByStartTime($a, $b) {
    return $a['start'] - $b['start'];
}

foreach($myArray as $key=>$array)
{
    usort($array, 'sortByStartTime');
    echo "<pre>";
    $newArray[$key] = $array;

}
print_r($newArray); 

<强>输出

Array
(
    [41] => Array
        (
            [0] => Array
                (
                    [event] => event1
                    [start] => 930
                    [end] => 1200
                )

            [1] => Array
                (
                    [event] => event2
                    [start] => 1000
                    [end] => 1230
                )

            [2] => Array
                (
                    [event] => event3
                    [start] => 1100
                    [end] => 1300
                )

        )

    [52] => Array
        (
            [0] => Array
                (
                    [event] => event1
                    [start] => 930
                    [end] => 1030
                )

            [1] => Array
                (
                    [event] => event3
                    [start] => 1100
                    [end] => 1300
                )

        )

)

查看实时演示:Click Here

答案 1 :(得分:0)

你可以像这样使用:

foreach($attractieIDs as $id) {
    usort($slots[$id], function($a, $b) {
       return $a['start'] - $b['start'];
    });
}

我正在使用以下$attractieIDs$slots数组,它运行良好:

$attractieIDs = [41, 52];

$slots = [
    41 => [
        [
            "event" => "event1",
            "start" => 930,
            "end" => 1200
        ],
        [
            "event" => "event3",
            "start" => 1100,
            "end" => 1300
        ],
        [
            "event" => "event2",
            "start" => 1000,
            "end" => 1230
        ]
    ],
    52 => [
        [
            "event" => "event3",
            "start" => 1100,
            "end" => 1300
        ],
        [
            "event" => "event2",
            "start" => 1000,
            "end" => 1230
        ],
        [
            "event" => "event1",
            "start" => 930,
            "end" => 1200
        ]
    ]
];