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条)

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错