以奇怪的格式对数组进行排序

时间:2015-06-05 22:14:01

标签: php arrays sorting

我有一个数组如下:

Array
(
    [time] => Array
        (
            [0] => 6:00 PM
            [1] => 4:00 PM
        )

[id] => Array
    (
        [0] => #1 Hits of the 60's
        [1] => 3 Men and a Lady
    )

[url] => Array
    (
        [0] => hits-of-the-60
        [1] => 3-men-and-a-lady
    )

[ev_evc_id] => Array
    (
        [0] => 2
        [1] => 2
    )

[address] => Array
    (
        [0] => Caravelle Theatre
        [1] => God and Country Theatre
    )

[phone] => Array
    (
        [0] => 1-800-4Branson (800-427-2676)
        [1] => 1-800-4Branson (800-427-2676)
    )

)

我想根据时间提升对数组进行排序。

所需的输出如下:

Array
(
    [time] => Array
        (
            [0] => 4:00 PM
            [1] => 6:00 PM
        )
[id] => Array
    (
        [0] => 3 Men and a Lady
        [1] => #1 Hits of the 60's
    )

[url] => Array
    (
        [0] => 3-men-and-a-lady
        [1] => hits-of-the-60
    )

[ev_evc_id] => Array
    (
        [0] => 2
        [1] => 2
    )

[address] => Array
    (
        [0] => God and Country Theatre
        [1] => Caravelle Theatre
    )

[phone] => Array
    (
        [0] => 1-800-4Branson (800-427-2676)
        [1] => 1-800-4Branson (800-427-2676)
    )

)

我试过了asort, usort。但无法使其发挥作用。任何帮助/建议表示赞赏。

2 个答案:

答案 0 :(得分:1)

您的阵列顺序错误。如果您可以将其更改为此并尝试使用

,请尝试
Array
(
    [0] => Array
        (
            [time] => 6:00 PM
            [id] => 1
            ...
        )
    [1] => Array
        (
            [time] => 4:00 PM
            [id] => 2
            ...
        )
)

答案 1 :(得分:1)

您可以使用array_multisort执行此操作,它将根据给定的第一个数组对所有数组进行排序:

array_multisort(
    $myArray["time"],
    $myArray["id"],
    $myArray["url"],
    $myArray["ev_evc_id"],
    $myArray["address"],
    $myArray["phone"]
);