How to sort a JSON array from PHP
First of all, I'm a complete newbie to this, I've been looking for solutions, but no one seems to be doing the trick.
So, I'm trying to sort this JSON array by date, but I really don't know what I should be solving, any advice in the right direction is greatly appreciated!
["info":[
{"id":1, "title":"original title", "name":"john doe", "date":"2010-05-15"},
{"id":2, "title":"another title", "name":"foo bar", "date":"2009-04-11"},
...
So I am getting data like this
$data=file_get_contents('jsondata...');
$d=json_decode($data,true);
I would like to sort the data by date, any ideas how should I approach this? Is it possible to return only the year value? So the output will be 2009 instead of 2009-04-11?
Thank you in advance
source to share
You can use a custom comparison function as well: usort
$data = '{"info":[{"id":1, "title":"original title", "name":"john doe", "date":"2010-05-15"}, {"id":2, "title":"another title", "name":"foo bar", "date":"2009-04-11"}]}';
$info = json_decode($data, true)['info'];
usort($info, function ($a, $b) {
return $a['date'] <=> $b['date'];
});
<=>
works with strings because string comparison is also date comparison when your dates are formatted as YYYY-MM-DD.
Then, to show the value of the year for the entry, you can parse the date in DateTime
and reformat it:
$date = DateTime::createFromFormat('Y-m-d', $item['date']);
$year = $date->format('Y');
source to share
Use uasort to sort an array with a custom function and strtotime to parse a date against a timestamp.
$json = '
{"info":[
{"id":1, "title":"original title", "name":"john doe", "date":"2010-05-15"},
{"id":2, "title":"another title", "name":"foo bar", "date":"2009-04-11"}
]
}';
$arr = json_decode($json)->info;
uasort($arr, function($item1, $item2){
return strtotime($item1->date) > strtotime($item2->date);
});
Will output
Array
(
[1] => stdClass Object
(
[id] => 2
[title] => another title
[name] => foo bar
[date] => 2009-04-11
)
[0] => stdClass Object
(
[id] => 1
[title] => original title
[name] => john doe
[date] => 2010-05-15
)
)
source to share
Something like the following should start:
<?php
function sortDate($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
}
$input = array('info' => array(array('id' => 2, 'date' => '2019-04-11'), array('id' => 1, 'date' => '2010-05-15')));
usort($input['info'], 'sortDate');
print_r($input);
source to share
What if I want to sort the array below
[
{
"roleId": 1,
"role": "admin",
"featureId": 1,
"feature": "Dashboard",
"tenantId": 1,
"tenant": "Admin"
},
{
"roleId": 1,
"role": "admin",
"featureId": 2,
"feature": "Overview",
"tenantId": 1,
"tenant": "Admin"
},
{
"roleId": 1,
"role": "admin",
"featureId": 3,
"feature": "Devices",
"tenantId": 1,
"tenant": "Admin"
},
{
"roleId": 1,
"role": "admin",
"featureId": 4,
"feature": "Map View",
"tenantId": 1,
"tenant": "Admin"
}
]
into something similar
[
{
"roleId": 1,
"role": "admin",
"featureId": 1,
"feature": "Dashboard",
"featureId": 2,
"feature": "Overview",
"featureId": 4,
"feature": "Map View",
"featureId": 3,
"feature": "Devices",
"tenantId": 1,
"tenant": "Admin"
}
]
source to share