I'm reading a multi-dimensional array from JSON. I then need to sort based on two of the parameters, about 3 levels deep in the array.
I've tried array_multisort, but could only do one level at a time. I then moved to usort, based on several examples I saw here on stackoverflow, but it stubbornly refuses to work.
JSON:
[
{
"multiple parameters": "foobar",
"projects": [
{
"id": "00101",
"date": "9",
"time": "14:00",
"duration":"30"
},
{
"id": "EX001",
"date": "8",
"time": "13:30",
"duration":"15"
},
{
"id": "9A200",
"date": "10",
"time": "8:45",
"duration":"15"
},
{
"id": "EQ002",
"date": "8",
"time": "9:30",
"duration":"15"
}
]
}
]
PHP:
//read data from the json file
$theschedule = '/directory/path/schedule.json';
//read json file
if (file_exists ($theschedule)){
$contents = utf8_encode(file_get_contents($theschedule));
$Schedule= json_decode($contents, true);
//Sort array
usort($Schedule[0]['projects'], 'order_by_date_time');
function order_by_date_time($a, $b)
{
if ($a['date'] == $b['date'])
{
// date is the same, sort by time
if ($a['time'] == $b['time']) return 0;
return $a['time'] == 'y' ? -1 : 1;
}
// sort the date first:
return $a['date'] < $b['date'] ? 1 : -1;
}
Each meeting has a date and time - I need to sort by date, and then time, to populate a meeting schedule page.
I've read many, many stackoverflow posts. The most helpful were Sort multidimensional array by multiple criteria
Sort an associative array in php with multiple condition (source of the 'order_by_date_time' function)
I've read the PHP manual on usort at http://www.php.net/manual/en/function.usort.php (and many of the other array sort functions).
I've also validated the JSON with JSONLint, so I don't think it's the problem - but if that might be the problem, I can change it as well.
I know related questions have been raised here before - I've read so many of the posts, and tried so many of the suggested answers. There's some piece missing in my code, though, that I just can't see.