I have the following code to sort the items based on a timestamp.
$arr = [];
foreach(glob('*.json') AS $item) {
$data = json_decode(file_get_contents($item), true);
$arr[] = [
'taken' => $data['taken'],
'file-name' => $data['file-name'],
'account' => (empty($data['account']) ? null : $data['account'])
];
}
usort($arr, function($x, $y) { return $x['taken'] - $y['taken']; });
Here is the content of 4 of these JSON files I have in my folder:
{"hash":"1b668ef9398fe5fc9f68a3ad87d04a77","account":null,"file-name":"DSC00045","place":null,"taken":1663759434,"uploaded":1759397192},{"hash":"bccfb4221adc1e00b4fa34eef309d025","account":null,"file-name":"DSC00170","place":null,"taken":1663760473,"uploaded":1759397201},{"hash":"5aa05170b8e6c21b947db1a20b5a93ac","account":null,"file-name":"DSC00230","place":null,"taken":1663773888,"uploaded":1759397227},{"hash":"8de553beeda575d16377e62e77183507","account":null,"file-name":"DSC00318","place":null,"taken":1547596822,"uploaded":1759397265}
I want to get the previous and next item based on taken
(which is the timestamp).
Currently I have made a mess and used the following to get the current item:
foreach($arr_test AS $test) {
if($test['file-name'] == $get_image) {
$current = $test;
}
}
var_dump($current);
$get_item
is the variable for (isset($_GET['img']) ? safetag($_GET['img']) : null)
which contains the file name.
But now I am stuck. I have no clue what so ever how to get the previous and next item based on the timestamp.
My goal is to sort all items from the first foreach based on taken
, and then get current, previous, and next file name based on when the photos were taken no matter how they are sorted in the folder. It’s for a photo gallery.