duankanyi6539 2013-07-18 10:15
浏览 43
已采纳

在PHP中对多维数组进行排序

I have this multidimensional array, called $rent:

Array
(
    [product2] => Array
        (
            [dates] => Array
                (
                    [2013-07-25] => 2
                    [2013-07-23] => 1
                    [2013-07-21] => 3
                )

        )

    [product3] => Array
        (
            [dates] => Array
                (
                    [2013-07-24] => 5
                    [2013-07-22] => 4
                    [2013-07-20] => 3
                )

        )

    [product1] => Array
        (
            [dates] => Array
                (
                    [2013-07-29] => 1
                    [2013-07-28] => 2
                    [2013-07-27] => 2
                )

        )

)

I'd like to do a double sort:

  1. First, by productX ascending
  2. Then, for each product, by date of rent ascending

So that the resulting array would be:

Array
(
    [product1] => Array
        (
            [dates] => Array
                (
                    [2013-07-27] => 2
                    [2013-07-28] => 2
                    [2013-07-29] => 1
                )

        )

    [product2] => Array
        (
            [dates] => Array
                (
                    [2013-07-21] => 3
                    [2013-07-23] => 1
                    [2013-07-25] => 2
                )

        )

    [product3] => Array
        (
            [dates] => Array
                (
                    [2013-07-20] => 3
                    [2013-07-22] => 4
                    [2013-07-24] => 5
                )

        )

)

How can I reach this? Many thanks in advance

  • 写回答

3条回答 默认 最新

  • dongtang3155 2013-07-18 10:27
    关注

    try this:

    ksort($rent);
    foreach($rent as &$item) {
        ksort($item['dates']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 将USDZ文件转化为带颜色的OBJ文件
  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题
  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图
  • ¥15 用visual Studio 写c ++只运行上一个旧代码的运行结果是怎么回事