如何在PHP中对多维数组进行排序,其中键实际上是值

如何在PHP中对多维数组进行排序,其中键实际上是值

问题描述:

I have a multi-dimensional array shown below that I want to sort by the 1st level dimension and then the 2nd level dimension within the 1st level.

I want the dates to be in chronological order and then then times, within the dates, to be in clock sequence.

Array
(
    [2014-05-17] => Array
        (
            [0] => 17:30
            [1] => 11:30
            [2] => 13:30
            [3] => 12:30
            [4] => 12:30
        )

    [2014-06-21] => Array
        (
            [0] => 17:30
            [1] => 10:30
            [2] => 13:30
            [3] => 09:30
            [4] => 12:30
            [5] => 09:30
            [6] => 12:30
        )

    [2014-05-18] => Array
        (
            [0] => 17:30
            [1] => 11:30
            [2] => 13:30
            [3] => 12:30
            [4] => 12:30
        )
)

Also, after I sort the times, I want to remove duplicates -- in other words I simply want the unique times within each date to be in clock order.

Here is what I would like to produce:

[2014-05-17] => Array ( [0] => 11:30 [1] => 12:30 [2] => 13:30 [3] => 17:30 )

[2014-05-18] => Array ( [0] => 11:30 [1] => 12:30 [2] => 13:30 [3] => 17:30 )

[2014-06-21] => Array ( [0] => 09:30 [1] => 10:30 [2] => 12:30 [3] => 13:30 [4] => 17:30 )

我有一个如下所示的多维数组,我希望按第一级维度排序,然后排序第二级 第1级中的维度。 p>

我希望日期按时间顺序排列,然后在日期内按时间顺序排列。 p> Array ( [2014-05-17] =>数组 ( [0] => 17:30 [1] => 11:30 [ 2] => 13:30 [3] => 12:30 [4] => 12:30 ) [2014-06-21] =>数组 ( [0] => 17:30 [1] => 10:30 [2] => 13:30 [3] => 09:30 [4] => 12:30 [5] => 09:30 [6] => 12:30 ) [2014-05-18] =>数组 (\ n [0] => 17:30 [1] => 11:30 [2] => 13:30 [3] => 12:30 [4] => ; 12:30 ) ) )\ n code> pre>

此外,在我对时间进行排序之后,我想删除重复项 - 换句话说,我只是希望每个日期中的唯一时间按时钟顺序排列。

以下是我想要制作的内容: strong> p>

[2014-05-17] => 数组 ( [0] => 11:30 [1] => 12:30 [2] => 13:30 [3] => 17:30 ) p>

[2014-05-18] => 数组 ( [0] => 11:30 [1] => 12:30 [2] => 13:30 [3] => 17:30 ) p>

[2014-06-21] => 数组 ( [0] => 09:30 [1] => 10:30 [2] => 12:30 [3] => 13:30 [4] => 17:30 ) p> div>

If your variable array name is $arrayDate, execute this:

ksort($arrayDate);
foreach($arrayDate as &$key){
    $key = array_unique($key);
    sort($key);
}