How to sort this php array?

3

I have this array:

$res = array();
$res[] = array("16/08/2013", "13:32", "ROBERTO");
$res[] = array("16/08/2013", "13:16", "AMANDA");
$res[] = array("14/08/2013", "12:36", "SILMARA");
$res[] = array("14/08/2013", "", "ROBERTO");
$res[] = array("14/08/2013", "", "KATIA");
$res[] = array("13/10/2015", "16:47", "ROBERTO");
$res[] = array("14/08/2013", "", "SILMARA");
$res[] = array("14/08/2013", "", "AMANDA");

I need to sort by date and time, I'm using sort ($ res) but it does not sort as I need. Blank time should come first according to date.

    
asked by anonymous 09.07.2016 / 23:25

2 answers

5

The solution is to use the function usort or uasort , which use a callback to use specific sort rules:

usort( $res, 'compara' );
uasort( $res, 'compara' );

In the above case, we are saying that the order defines the function compara .

The basic difference between the two is that uasort preserves the original indexes, and usort does not.

The function whose name is passed in usort or uasort is called successively with two original array items by time, and you must return 0 , a negative number or a number positive for the case of tie, in order or out of order respectively.

In turn, within the compara() function, we will fix the order of its "date" and concatenate with time, and then return the comparison between the resulting strings using strcmp , which just returns 0 +n and -n as desired:

function compara( $a1, $a2 ) {
    $ts1 = substr($a1[0],6,4).substr($a1[0],3,2).substr($a1[0],0,2).$a1[1];
    $ts2 = substr($a2[0],6,4).substr($a2[0],3,2).substr($a2[0],0,2).$a2[1];
    return strcmp($ts1, $ts2);
}

See working with uasort on IDEONE . Compare with usort , also in IDEONE .


Just to better detail the above function, see how the strings are mounted:

item [0]                  14/08/2013
posição                   0123456789
quantidade de caracteres  2c 2c 4c
item [1]                  12:36
item [2]                  SILMARA 


 substr($a1[0],6,4) . substr($a1[0],3,2) . substr($a1[0],0,2) . $a1[1]
 └───── ANO ──────┘   └───── MÊS ──────┘   └───── DIA ──────┘  └ HORA ┘
└────────────────────────── Item 0 ──────────────────────────┘└ Item 1 ┘

Resultado                 2013081412:36


PHP Manual:

09.07.2016 / 23:45
2

I'm a bit late, but you can do this too: Using array_multisort

<?php
//array
$res = array();
$res[] = array("16/08/2013", "13:32", "ROBERTO");
$res[] = array("30/01/2011", "13:16", "AMANDA");
$res[] = array("14/07/2016", "12:36", "SILMARA");
$res[] = array("14/05/2013", "", "ROBERTO");
$res[] = array("14/09/2013", "", "KATIA");
$res[] = array("13/10/2015", "16:47", "ROBERTO");
$res[] = array("14/08/2013", "", "SILMARA");
$res[] = array("14/08/2011", "", "AMANDA");

//percorre as chaves e valores
foreach ($res as $key => $row) {
$ndata[$key] = $row[0];
//Inverte a data
$ndata[$key] = implode("/",array_reverse(explode("/",$ndata[$key])));
$hora[$key] = $row[1];
}
//ordena
array_multisort($ndata, SORT_DESC, $hora, SORT_ASC,$res);

//saída
echo"<pre>";
var_dump ($res);
echo"</pre>";
?>

Output

array(8) {
[0]=>
array(3) {
[0]=>
string(10) "14/07/2016"
[1]=>
string(5) "12:36"
[2]=>
string(7) "SILMARA"
}
[1]=>
array(3) {
[0]=>
string(10) "13/10/2015"
[1]=>
string(5) "16:47"
[2]=>
string(7) "ROBERTO"
}
[2]=>
array(3) {
[0]=>
string(10) "14/09/2013"
[1]=>
string(0) ""
[2]=>
string(5) "KATIA"
}
[3]=>
array(3) {
[0]=>
string(10) "16/08/2013"
[1]=>
string(5) "13:32"
[2]=>
string(7) "ROBERTO"
}
[4]=>
array(3) {
[0]=>
string(10) "14/08/2013"
[1]=>
string(0) ""
[2]=>
string(7) "SILMARA"
}
[5]=>
array(3) {
[0]=>
string(10) "14/05/2013"
[1]=>
string(0) ""
[2]=>
string(7) "ROBERTO"
}
[6]=>
array(3) {
[0]=>
string(10) "14/08/2011"
[1]=>
string(0) ""
[2]=>
string(6) "AMANDA"
}
[7]=>
array(3) {
[0]=>
string(10) "30/01/2011"
[1]=>
string(5) "13:16"
[2]=>
string(6) "AMANDA"
}
}

Is that it? Now it's certim ...

    
10.07.2016 / 01:05