Order this array by date?

I have a php file that creates an array of everything in my user directory, then the array is sent back to the iPhone.

The array created by my php arranges them in alphabetical order, I want it to be sorted by file creation date.

This is what my php file looks like

<?php
$username = $_GET['username'];
$path = "$username/default/";


$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);

// output to json
echo json_encode($files);

?>

How should I do it?

Thank:)

+2
source share
2 answers

Using usort()with a callback that calls filemtime()...

This has not been verified, but I believe that it will set you on the right path ...

// First define a comparison function to be used as a callback
function filetime_callback($a, $b)
{
  if (filemtime($a) === filemtime($b)) return 0;
  return filemtime($a) < filemtime($b) ? -1 : 1; 
}

// Then sort with usort()
usort($files, "filetime_callback");

-. , - < > return.

+5

, usort() - , (.. ),

usort($files, function ($a, $b){
    if (filemtime($a) === filemtime($b)) return 0;
    return filemtime($a) < filemtime($b) ? -1 : 1; 
});

, .

, .

0

Source: https://habr.com/ru/post/1536031/


All Articles