在php中使用两个索引键对多维数组进行排序

时间:2015-12-09 13:04:14

标签: php arrays sorting multidimensional-array

我想用两个索引键对多维数组进行排序。 让我举个例子,

这里是我通过查询获得的内容。

Array
(
    [0] => Array
        (
            [cmp_id] => 33
            [cmp_name] => CONF
            [mod_pin] => 6811
            [provider_name] => test2
            [meeting_count] => 68
            [total_participant] => 123
            [total_dur] => 353:25:00
            [web_dur] => 575.19
            [did_dur] => 1.78
        )

    [1] => Array
        (
            [cmp_id] => 33
            [cmp_name] => MCONF
            [mod_pin] => 1908
            [provider_name] => test2
            [meeting_count] => 4
            [total_participant] => 7
            [total_dur] => 10:19:00
            [web_dur] => 17.7
            [did_dur] => 0
        )

    [2] => Array
        (
            [cmp_id] => 1
            [cmp_name] => MT0001
            [mod_pin] => 1662
            [provider_name] => mob_test
            [meeting_count] => 3
            [total_participant] => 5
            [total_dur] => 20:18:00
            [web_dur] => 22.05
            [did_dur] => 0
        )

    [3] => Array
        (
            [cmp_id] => 1
            [cmp_name] => MT0001
            [mod_pin] => 1234
            [provider_name] => mob_test
            [meeting_count] => 4
            [total_participant] => 10
            [total_dur] => 16:40:00
            [web_dur] => 17.77
            [did_dur] => 0
        )

)

我想按provider_name而不是mod_pin对其进行排序。

它想要这样

Array
(
    [0] => Array
        (
            [cmp_id] => 33
            [cmp_name] => CONF
            [mod_pin] => 1908
            [provider_name] => test2
            [meeting_count] => 4
            [total_participant] => 7
            [total_dur] => 10:19:00
            [web_dur] => 17.7
            [did_dur] => 0
        )
    [1] => Array
        (
            [cmp_id] => 33
            [cmp_name] => CONF
            [mod_pin] => 6811
            [provider_name] => test2
            [meeting_count] => 68
            [total_participant] => 123
            [total_dur] => 353:25:00
            [web_dur] => 575.19
            [did_dur] => 1.78
        )

[2] => Array
        (
            [cmp_id] => 1
            [cmp_name] => MT0001
            [mod_pin] => 1234
            [provider_name] => mob_test
            [meeting_count] => 4
            [total_participant] => 10
            [total_dur] => 16:40:00
            [web_dur] => 17.77
            [did_dur] => 0
        )
    [3] => Array
        (
            [cmp_id] => 1
            [cmp_name] => MT0001
            [mod_pin] => 1662
            [provider_name] => mob_test
            [meeting_count] => 3
            [total_participant] => 5
            [total_dur] => 20:18:00
            [web_dur] => 22.05
            [did_dur] => 0
        )
)

提前致谢。

1 个答案:

答案 0 :(得分:0)

  

您需要array_multisort

# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($myArray as $k=>$v) {
    $sort['provider_name'][$k] = $v['provider_name'];
    $sort['mod_pin'][$k] = $v['mod_pin'];
}
# sort by provider_name  and then title mod_pin
array_multisort($sort['provider_name'], SORT_ASC, $sort['mod_pin'], SORT_ASC,$mylist);