我有一个json
文件,里面有一个字段date
,它不是特定的格式,它应该保持这样。我想知道is如何用php对这个json排序,这样日期最近的记录就会出现在json的开头,以此类推,直到以降序结束。
我试过很多方法,但都不管用。
下面是json的外观:
[
{"fullName":"John Doe","userName":"user2",
"phone":"+1234124512","email":"asd@bv.com",
"date":"2016-11-03","time":"1pm","reason":"some reason",
"isApproved":1,"label":"success","status":"Approved"},
{"fullName":"Robert Royce","userName":"user1",
"phone":"+4460475640","email":"asd@asf.cv","date":"2016-11- 17",
"time":"130pm","reason":"some reason",
"isApproved":1,"label":"success","status":"Approved"},
{"fullName":"Pesho Zdr","userName":"user4",
"phone":"+4560477640","email":"grozdan@abv.bg","date":"2016-11-17",
"time":"130pm","reason":"some reason",
"isApproved":0,"label":"success","status":"Approved"}
]
1条答案
按热度按时间eimct9ow1#
第一步
首先将json转换为
json_decode
数组第二个参数用于指示输出需要是数组而不是对象
第二步
对数组进行排序;看起来
usort
可以帮助您这将从第一个值到最后一个值遍历数组,比较第一个值(
$a
)与第二个值($b
)。我们尤其对
date
值感兴趣。要使
usort
按预期工作,需要从闭包中返回一个值以告知比较结果。第三步
最后,使用
json_encode
将所有内容转换回json格式