Sort two-dimensional array by date index (timestamp)

Asked

Viewed 125 times

2

I got the following array that contains file information from a folder:

    Array
    (
        [name] => Apresenta__o1.ppt
        [server_path] => C:\wamp\www\portais\arquivos\alunos\4_45258\files\Apresenta__o1.ppt
        [size] => 174080
        [date] => 1432943066
        [relative_path] => C:\wamp\www\portais\arquivos/alunos/4_45258/files
        [ext] => Array
            (
                [0] => application
                [1] => powerpoint
            )

        [i_empresa] => 4
        [i_aluno] => 45258
    )
    Array
    (
        [name] => Apresenta__o2.jpg
        [server_path] => C:\wamp\www\portais\arquivos\alunos\4_45258\files\Apresenta__o2.jpg
        [size] => 68710
        [date] => 1432943064
        [relative_path] => C:\wamp\www\portais\arquivos/alunos/4_45258/files
        [ext] => Array
            (
                [0] => image
                [1] => jpeg
            )

        [i_empresa] => 4
        [i_aluno] => 45258
    )
    Array
    (
        [name] => Atendimento_Comunicativo.doc
        [server_path] => C:\wamp\www\portais\arquivos\alunos\4_45258\files\Atendimento_Comunicativo.doc
        [size] => 28672
        [date] => 1434671499
        [relative_path] => C:\wamp\www\portais\arquivos/alunos/4_45258/files
        [ext] => Array
            (
                [0] => application
                [1] => msword
            )

        [i_empresa] => 4
        [i_aluno] => 45258
    )

How to order this array by index date?

2 answers

1


You can use the function usort and create a role for the rule:

Numeric Value:

usort($array, function ($a, $b){
    return $a['date'] - $b['date'];
});

String:

usort($array, function ($a, $b){
    strcmp($a['name'], $b['name']);
});

Fiddle

Source

-4

Try it this way:

$array_total = array($array_com_informacoes_1, $array_com_informacoes_2...);

arsort($array_total); //mais recentes primeiro
            ou
asort($array_total); //mais antigos primeiro

var_dump($array_total); //Agora vc tem um array com todos os outros, de forma ordenada

code example:

    $itens = array(Array
(
    'name' => 'Apresenta__o2.jpg',
    'server_path' => 'C:\wamp\www\portais\arquivos\alunos\4_45258\files\Apresenta__o2.jpg',
    'size' => 68710,
    'date' => 1432943064,
    'relative_path' => 'C:\wamp\www\portais\arquivos/alunos/4_45258/files',
    'ext' => Array
        (
            '0' => 'image',
            '1' => 'jpeg'
        ),

    'i_empresa' => 4,
    'i_aluno' => 45258,
),
Array
(
    'name' => 'Atendimento_Comunicativo.doc',
    'server_path' => 'C:\wamp\www\portais\arquivos\alunos\4_45258\files\Atendimento_Comunicativo.doc',
    'size' => 28672,
    'date' => 1434671499,
    'relative_path' => 'C:\wamp\www\portais\arquivos/alunos/4_45258/files',
    'ext' => Array
        (
            '0' => 'application',
            '1' => 'msword'
        ),

    'i_empresa' => 4,
    'i_aluno' => 45258
));
arsort($itens);//Mais recentes primeiro 
var_dump($itens);   
  • did not work, returns 1 for each array

  • The functions above, sort by reference, so you can try this: Put all the returned arrays in a single array, then use one of the functions above to organize this array, pronto vc has an array with all the other ordered within it, I will edit the answer...

  • tried all these ways and all return 1 and not the reordered array

  • nothing, the same return 1 always

Browser other questions tagged

You are not signed in. Login or sign up in order to post.