如何对多维数组进行排序

Z明军
  • 80
{
    "data":[
        {
            "id":"486",
            "time":"1517310441",
            "content":"",
            "thumb":[
                {
                    "url":"https://123win.oss-cn-shenzhen.aliyuncs.com//upload/IMG_6598.JPG"
                },
                {
                    "url":"https://123win.oss-cn-shenzhen.aliyuncs.com//upload/IMG_6535.JPG"
                },
                {
                    "url":"https://123win.oss-cn-shenzhen.aliyuncs.com//upload/IMG_6534.JPG"
                }
            ],
            "nickname":"咸鱼",
            "headimgurl":"https://123win.oss-cn-shenzhen.aliyuncs.com//upload/p-7a6ec679.jpg",
            "uid":"10668",
            "like":"2",
            "rew":0,
            "smit":0,
            "type":3
        }
    ],
    "comment":[

    ],
    "rew":[
        {
            "ccl_rew":"最爱矿石奇景,历史建筑中温暖的家,蕴含着数代领袖气息和智慧结晶。",
            "id":"481",
            "time":"0"
        },
        {
            "ccl_rew":"测试评论",
            "id":"489",
            "time":"0"
        }
    ]
}

像这种数组格式的,如何用里面的time字段进行排序,因为项目要做一个类似通知的集合

回复
阅读 1.3k
5 个回答
data = data.sort((a, b) => a.time > b.time ? -1 : 1)

参考菠菜园这个方法https://www.zkii.net/tech/php... ,多维数组,想咋排咋排。示例是个二维的,你可以自己扩展下,原理都一样

function my_array_multisort($data,$sort_order_field,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC){
foreach($data as $val){
$key_arrays[]=$val[$sort_order_field];
}
array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);
rturn $data;
}

楼主试试这样能不能达到你的需求。

data 与 rew 有对应关系么 没有的话直接sql中 order by time

你知道吗?

宣传栏