0

I have some difficult to learn array sort in php, and I need your help !

I have this array :

[fournisseurs_1331566668.csv] => Array
    (
        [name] => fournisseurs_1331566668.csv
        [date] => 1331566694
    )

[fournisseurs_1385460508.csv] => Array
    (
        [name] => fournisseurs_1385460508.csv
        [date] => 1385460517
    )

[fournisseurs_1334062493.csv] => Array
    (
        [name] => fournisseurs_1334062493.csv
        [date] => 1334062505
    )

I want to sort by the date (Descending order).

What function is to use ? I try with sort and array_multisort, but no result :(

Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
bahamut100
  • 1,795
  • 7
  • 27
  • 38
  • send the sort function to `array_map()` – Royal Bg Dec 12 '13 at 09:42
  • Could you be more specific when talking about your attempt to use array_multisort? Can we see the code? This function must have been helpful, but sometimes it is tricky to use. Probably, there's something wrong with your function call. – Alexander Taver Dec 12 '13 at 09:44
  • 1
    Check this: http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2 – Ana Claudia Dec 12 '13 at 09:46

2 Answers2

2

uasort would be a possibility http://www.php.net/manual/de/function.uasort.php

uasort($array, function($a, $b){
     if ($a['date'] == $b['date']) {
         return 0;
     }
     return ($a['date'] < $b['date']) ? +1 : -1;
});

example: http://3v4l.org/QSXOG

gherkins
  • 14,603
  • 6
  • 44
  • 70
1

I made this function to sort every multidimensional array by one of his columns :

 function sortArrayBy($array , $column_name,$sort=SORT_DESC){

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

    array_multisort($column, $sort, $array);
    return $array;

}

Call it like this :

<?php sortArrayBy($yourArray,'date') ; ?>
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44