My question is how to get or split an array in ascending order using array key date,
I tried a lot...but I didn't get it,
[ { "id": "47", "date": "07/16/2022", "text": "ph" } { "id": "46", "date": "06/16/2022", "text": "ph" }, { "id": "45", "date": "06/16/2021", "text": "ph" }]
The output I need is,
[ "2021": [{ "id": "45", "date": "06/16/2021", "text": "ph" }], "2022": [{ "id": "46", "date": "06/16/2022", "text": "ph" }, { "id": "47", "date": "07/16/2022", "text": "ip" }] ]
How to do this using PHP or JavaScript?
The PHP version might look like this:
As asked in Diego's answer, I also put
ksort
in it, which sorts the resulting array in descending order by key.Here is a demonstration on how to use JavaScript to convert an input array into an expected output object: