Saturday, October 17, 2015

Sorting multidimensional array

//Input
$files = Array
(
    

    [0] => Array
        (
            [filename] => 1.mp4
            [modified] => 0.00
        )

    [1] => Array
        (
            [filename] => video2
            [modified] => 1.00
        )

    [2] => Array
        (
            [filename] => video1
            [modified] => 2.00
        )

)

usort($files, array('UsersController','date_compare'));

$files is your multidimensional array
UsersController is your class name
date_compare is function

function date_compare($a, $b){
        $t1 = strtotime($a['modified']);
        $t2 = strtotime($b['modified']);
        return $t2 - $t1;
    }

Output
Array
(
    

    [0] => Array
        (
            [filename] => video1

            [modified] => 2.00
        )

    [1] => Array
        (
            [filename] => video2
            [modified] => 1.00
        )

    [2] => Array
        (
            [filename] => 1.mp4
            [modified] => 0.00
        )

)

No comments:

Post a Comment