-1

Im looking for a way to sort this array by the key "Created" so i get the newest record first. Anyone have an idea on how to do this?

My array contains about 2500 records and growing and is formated as follows:

array(2525) { 
    [0]=> array(1) { 
        [0]=> array(11) { 
            ["id"]=> int(0)
            ["Filename"]=> string(20) "011899988199119725-3"
            ["Uri_MP4"]=> string(35) "/v4/output/011899988199119725-3.mp4"
            ["Uri_PNG"]=> string(41) "/v4/output/thumb/011899988199119725-3.png"
            ["Uri_GIF"]=> string(29) "/gif/011899988199119725-3.gif"
            ["SizeMP4"]=> string(9) "391.44 KB"
            ["Created"]=> string(16) "2019-08-07 13:24"
            ["Width"]=> int(400)
            ["Height"]=> int(222)
            ["Duration"]=> string(4) "0:11"
        }
    }

i have tried:

$keys = array_column($array_, 'Created');
array_multisort($keys, SORT_DESC, $array_);

and

$sortedArray = Arr::sortByKeys($array_, 'Created');

and

function date_sort($a, $b) {
    return strtotime($a) - strtotime($b);
}
$test = usort($array_, "date_sort");
print_r($test);
  • 2
    Possible duplicate of [How to Sort Multi-dimensional Array by Value?](https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value) –  Aug 19 '19 at 22:19
  • Where does this array come from? It looks like a row from a database and if that's the case I'd sort it in the query. – Don't Panic Aug 19 '19 at 22:24
  • I have tried all the suggestions but no luck. Im thinking that it is because my date and time is a string? – Jimmy Oskarsson Aug 19 '19 at 22:25
  • No, with the datetime in a string in that format, it should still be directly sortable. – Don't Panic Aug 19 '19 at 22:26
  • The array comes from my script, i scan a folder for files and build the array in a foreach loop. $Movie = array( array("id"=>$id, "Filename"=>$filename, "Uri_MP4"=>$urimp4, "Uri_PNG"=>$uripng, "Uri_GIF"=>$urigif, "FilePath"=>$filepath, "SizeMP4"=>$sizemp4, "Created"=>$created, "Width"=>$x, "Height"=>$y, "Duration"=>$file['playtime_string']), ); – Jimmy Oskarsson Aug 19 '19 at 22:26

2 Answers2

0

If you convert your string dates to timestamps then you should be able to use usort() with a custom comparison function.

lets call your single element array parent.

foreach( $parent as $elem ) {
    $elem['Created'] = strtotime($elem['Created']);
}

Now we can write a comparison function to compare each element

function sort_dates($date1, $date2) {
    return $date1 - $date2;
}

and then call your comparison method with usort();

usort($parent, 'sort_dates');

The method above requires converting our string dates into timestamps. Let's take a look at a more consolidated approach where we can keep the string values but still get the sorting we want by using temporary variables:

function sort_dates($date1, $date2) {
    $datetime1 = strtotime($date1['Created']);
    $datetime2 = strtotime($date2['Created']);
    return $datetime1 - $datetime2;
}

usort($parent, 'sort_dates');

Here, we're simply converting the string dates to timestamps in temporary variables. This way we don't have to overwrite our string dates.

Is this what you were looking for?

kenef
  • 183
  • 10
0

For some reason, there appears to be an extra array level in the example output you showed. I believe that is one reason the sorting methods you've tried so far haven't worked.

Instead of

[
    [row],
    [row],
    [etc.],
]

You have

[
    [[row]],
    [[row]],
    [[etc.]],
]

If you fix the code that generates the array so that that extra array level is removed, the array_multisort method you tried will work.

The usort method will almost work; the only problem with that one is that you need to refer to the Created key in the callback. (You will also want to do b - a rather than a - b if you want descending order.)

function date_sort($a, $b) {
    return strtotime($b['Created']) - strtotime($a['Created']);
}
$test = usort($array_, "date_sort");

If you really do need the extra array level for some reason, you can still account for it in your comparison function.

function date_sort($a, $b) {
    return strtotime($b[0]['Created']) - strtotime($a[0]['Created']);
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80