S
S
sl1m_dogg2014-09-29 13:25:37
PHP
sl1m_dogg, 2014-09-29 13:25:37

PHP sorting by date not working What could be wrong?

Всем привет. Не могу отсортировать массив по дате. Дата - это дата создания файла. Помогите кто может, уже все интернеты облазил.

function array_sort_by_column($array, $column, $direction = SORT_ASC) {
$reference_array = array();
$i = 0;

foreach($array as $key => $row) {
$reference_array[$key] = $array[$column][$i];
$i++;
}

array_multisort($reference_array, $direction, $array);

return $array;
}

$array:

functions.php 43.30K 2014-09-29 09:42:21
wpdb_vooq.sql 479.09K 2014-09-29 11:31:23
simple-cart-buy-now(1).zip 149.48K 2014-09-29 09:43:00
jdk-8u20-linux-i586.tar.gz 154.87M 2014-09-29 10:36:40
isotope-master.zip 75.71K 2014-09-29 09:42:26
userinfo.php 14.96K 2014-09-29 09:48:02
default (1).php 13.96K 2014-09-29 11:04:38
yii-master.zip 10.38M 2014-09-29 11:31:38
gamma(1).rar 1.01M 2014-09-29 09:42:39
auremo_w_catalog_yii_rating.sql 1.53K 2014-09-29 09:45:56
boxes.pptx 38.14K 2014-09-29 09:42:34

$data['date'][] = date("Y-m-d H:i:s", filectime($dir.SEP.$value));

code that returns a list of files:
function treeList($dir = "") {
  $i = 0;
  if (is_dir($dir)) {
     if ($dh = opendir($dir)) {
         while (($file = readdir($dh)) !== false) {
         	if (!is_dir($file)){
             $data['file'][] = $file;
         	}
         }
         foreach ($data['file'] as $key => $value) {
         	$data['size'][] = human_filesize(filesize($dir.SEP.$value));
         	$data['date'][] = date("Y-m-d H:i:s", filectime($dir.SEP.$value));
         }
         closedir($dh);
     }
    array_sort_by_column($data, 'date');
  }

  return $data;
  }

Updated
the first three files when I load everything sorts the rules, and then in general in an incomprehensible order it displays
Updated 2
Undefined index: date in
function cmp($a, $b)
{
  $t1 = strtotime($a["date"]);
  $t2 = strtotime($b["date"]);

    return ($t2<$t1) ? -1 : 1;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-09-29
Protko @Fesor

It's kind of complicated for you.
1) why translate a number convenient for sorting into a string?
2) why not use usort/uasort?

$files = ['some.txt']; // например через glob получаем список файлов
$cTimes = array_map(function ($file) {
    return filectime($file);
}, $files);
$data = array_combine($files, $cTimes);
uasort($data, function ($a, $b) {
    return $a - $b;
});
$sortedFiles = array_keys($data);

O
Optimus, 2014-09-29
Pyan @marrk2

Maybe for reliability the date should be translated into timestamp and then sorted by it?

A
Alexey, 2014-09-29
@rdifb0

If you do not send the result of the function execution to the forefathers, then everything will probably be sorted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question