Sort Array by data time in php

Asked

Viewed 651 times

-1

Hello, I have already looked into the matter but I have not yet obtained anything that works properly. I have a database with multiple orders and these have a deadline and also a fixed date, but the deadline data cannot be saved in the database because they are always changing according to the date and time, and here comes the problem how I can sort this table, in order of increasing time date so that the indices ie, the line of each item can not be changed,

I thought something like usort, but it didn’t work...

while ($pRow = mysqli_fetch_object($pQuery)) {
        $ped_id[$w] = $pRow->ped_id;
        $ped_cliente[$w] = $pRow->ped_cliente;
        $ped_entrega[$w] = $data_hs_entrega_print;
        $ped_msg[$w] = $msg;
        $ped_status[$w] = $status;
        $cor[$w] = $cor_chamado;
        $classe[$w] = $class;
        $botoes[$w] = $botao;
        $w++;
}
function sortFunction( $a, $b ) {
    return strtotime($a) - strtotime($b);
}
for($g=0;$g<$w;$g++){
    $array['ped_id'][$g] = $ped_id[$g];
    $array['ped_cliente'][$g] = $ped_cliente[$g];
    $array['ped_entrega'][$g] = $ped_entrega[$g];
    $array['ped_msg'][$g] = $ped_msg[$g];
    $array['ped_status'][$g] = $ped_status[$g];
    $array['cor'][$g] = $cor[$g];
    $array['classe'][$g] = $classe[$g];
    $array['botoes'][$g] = $botoes[$g];

    usort($array['ped_entrega'], "sortFunction");
}

1 answer

0


Just change the date item to the first Array item and apply arsort(http://php.net/manual/en/function.arsort.php), then take the already ordered key and put in a for to change the position of the array items;

    for($g=0;$g<$w;$g++){
        //APagando a Hora
        $teste_data = trim($ped_entrega[$g]);
        if(strstr($teste_data," - ")){
            $vet1 = explode(" - ",$teste_data);
            $data_arr = $vet1[0];
        }
        //Colocando o Formato data Y-m-d
        $teste_data2 = trim($data_arr);
        if(strstr($teste_data2,"/")){
            $vet2 = explode("/",$teste_data2);
            $data_arr2 = $vet2[2] . "-" . $vet2[1] . "-" . $vet2[0];
        }

        $array[$g]['data'] = date('Y-m-d', strtotime($data_arr2));
        $array[$g]['id'] = $ped_id[$g];
        $array[$g][2] = $ped_cliente[$g];
        $array[$g][3] = $ped_entrega[$g];
        $array[$g][4] = $data_hs_entrega[$g];
        $array[$g][5] = $ped_msg[$g];
        $array[$g][6] = $ped_status[$g];
        $array[$g][7] = $cor[$g];
        $array[$g][8] = $classe[$g];
        $array[$g][8] = $botoes[$g];
        $array['minutos'][9] = $minutos_array[$g]; //Ordenação do dia por Minutos . . .
    }

arsort($array); 
$e=0;

foreach ($array as $key => $val) {
    $posicao[$e] = $key;
    $e++;
}

for($h=0;$h<$w;$h++){
    $c = $posicao[$h];
    echo $array[$c][3] . "<br/>";
}

Browser other questions tagged

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