du4822 2017-10-24 13:56
浏览 70
已采纳

在PHP中排序Json数据? 不同的具体数据?

This is json data in webservice, I need to sort this kind of information by top to date sort 2017..bla - 2015..bla -2014, maybe sort value "4" or title 2015 blabla.... value in a PHP script, how can I do?

"2015-09-11 09:18:11.023151": {
    "1": "4422",
    "2": "asd",
    "3": "aaa",
    "4": "2015-09-11 09:18:11.023151"
    },
"2014-10-29 13:33:14.325827": {
    "1": "5852",
    "2": "shfe",
    "3": "sds",
    "4": "2014-10-29 13:33:14.325827"
},
"2017-11-10 09:18:11.315102": {
    "1": "2323",
    "2": "sfd",
    "3": "sdf",
    "4": "2017-11-10 09:18:11.315102"
}

i try :

$example= json_decode($result, true);


usort($example, function($a, $b) {  
    return $a->{"4"} > $b->{"4"} ? -1 : 1; }); 

echo json_encode($example);

how we can do it ?

  • 写回答

2条回答 默认 最新

  • duan5801 2017-10-24 14:16
    关注

    Please, try this.

    $result = '{"2015-09-11 09:18:11.023151":{"1":"4422","2":"asd","3":"aaa","4":"2015-09-11 09:18:11.023151"},"2014-10-29 13:33:14.325827":{"1":"5852","2":"shfe","3":"sds","4":"2014-10-29 13:33:14.325827"},"2017-11-10 09:18:11.315102":{"1":"2323","2":"sfd","3":"sdf","4":"2017-11-10 09:18:11.315102"}}';
    
    $example= json_decode($result, true);
    usort($example, function($a, $b) { return $a["4"] > $b["4"] ? -1 : 1; }); 
    echo json_encode($example);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器