欢迎您, 来到 宁时修博客.^_^

PHP二维数组按指定key值进行排序

2018/04/25 宁时修 php 781
array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。

array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。

下面是使用案例:

Array
(
    [1523721600] => Array
        (
            [already_total] => 308
            [total] => 1420
            [none_total] => 1112
            [time] => 2018-04-15
        )
    [1523808000] => Array
        (
            [already_total] => 2605
            [total] => 4673
            [none_total] => 2068
            [time] => 2018-04-16
        )
    [1523894400] => Array
        (
            [already_total] => 886
            [total] => 1077
            [none_total] => 191
            [time] => 2018-04-17
        )
    [1523980800] => Array
        (
            [already_total] => 9536959
            [total] => 9537689
            [none_total] => 730
            [time] => 2018-04-18
        )
    [1524067200] => Array
        (
            [already_total] => 579244
            [total] => 905494
            [none_total] => 326250
            [time] => 2018-04-19
        )
    [1524153600] => Array
        (
            [already_total] => 9697
            [total] => 9697
            [none_total] => 0
            [time] => 2018-04-20
        )
    [1524499200] => Array
        (
            [already_total] => 0
            [total] => 39
            [none_total] => 39
            [time] => 2018-04-24
        )
    [1524585600] => Array
        (
            [already_total] => 0
            [total] => 29
            [none_total] => 29
            [time] => 2018-04-25
        )
    [1524240000] => Array
        (
            [already_total] => 0
            [total] => 0
            [none_total] => 0
            [time] => 2018-04-21
        )
    [1524326400] => Array
        (
            [already_total] => 0
            [total] => 0
            [none_total] => 0
            [time] => 2018-04-22
        )
    [1524412800] => Array
        (
            [already_total] => 0
            [total] => 0
            [none_total] => 0
            [time] => 2018-04-23
        )
)

下面我要做的是,想根据二维数组中的time-key值进行排序,进行升序操作。执行下面代码

// 对二位数组 准备要排序的数组
 foreach ($old as $k => $v) {
        $edition[] = $v['time'];
}
array_multisort($edition, SORT_ASC, $old);

执行后的结果:

Array
(
    [0] => Array
        (
            [already_total] => 308
            [total] => 1420
            [none_total] => 1112
            [time] => 2018-04-15
        )
    [1] => Array
        (
            [already_total] => 2605
            [total] => 4673
            [none_total] => 2068
            [time] => 2018-04-16
        )
    [2] => Array
        (
            [already_total] => 886
            [total] => 1077
            [none_total] => 191
            [time] => 2018-04-17
        )
    [3] => Array
        (
            [already_total] => 9536959
            [total] => 9537689
            [none_total] => 730
            [time] => 2018-04-18
        )
    [4] => Array
        (
            [already_total] => 579244
            [total] => 905494
            [none_total] => 326250
            [time] => 2018-04-19
        )
    [5] => Array
        (
            [already_total] => 9697
            [total] => 9697
            [none_total] => 0
            [time] => 2018-04-20
        )
    [6] => Array
        (
            [already_total] => 0
            [total] => 0
            [none_total] => 0
            [time] => 2018-04-21
        )
    [7] => Array
        (
            [already_total] => 0
            [total] => 0
            [none_total] => 0
            [time] => 2018-04-22
        )
    [8] => Array
        (
            [already_total] => 0
            [total] => 0
            [none_total] => 0
            [time] => 2018-04-23
        )
    [9] => Array
        (
            [already_total] => 0
            [total] => 39
            [none_total] => 39
            [time] => 2018-04-24
        )
    [10] => Array
        (
            [already_total] => 0
            [total] => 29
            [none_total] => 29
            [time] => 2018-04-25
        )
)


点赞
说说你的看法

所有评论: (0)