i tried to sort a multidimensional array with PHP array_multisort with no success. here is my multidimensional array
array(1) {
["categories"]=>
array(9) {
[0]=>
array(9) {
["colonia"]=>
string(10) "Cuajimalpa"
["municipio"]=>
string(21) "Cuajimalpa de Morelos"
["id"]=>
string(2) "29"
["gps_lat"]=>
string(7) "19.3563"
["gps_lon"]=>
string(8) "-99.2835"
["distance"]=>
string(17) "16.57901296536604"
["Nombre"]=>
string(24) "SERGIO TENOPALA VILLEGAS"
["tiempo"]=>
string(6) "42 min"
["distancia"]=>
string(7) "31,6 km"
}
[1]=>
array(9) {
["colonia"]=>
string(25) "Culhuac�n CTM Secci�n III"
["municipio"]=>
string(8) "Coyoac�n"
["id"]=>
string(2) "33"
["gps_lat"]=>
string(9) "19.325428"
["gps_lon"]=>
string(9) "-99.11923"
["distance"]=>
string(17) "2.662658358779893"
["Nombre"]=>
string(30) "GLORIA IDALIA CABRERA MART�NEZ"
["tiempo"]=>
string(6) "13 min"
["distancia"]=>
string(6) "4,0 km"
}
[2]=>
array(9) {
["colonia"]=>
string(18) "Escand�n I Secci�n"
["municipio"]=>
string(14) "Miguel Hidalgo"
["id"]=>
string(2) "28"
["gps_lat"]=>
string(7) "19.3998"
["gps_lon"]=>
string(7) "-99.173"
["distance"]=>
string(17) "7.554089206711326"
["Nombre"]=>
string(22) "GABRIEL MEJ�A TERRAZAS"
["tiempo"]=>
string(6) "30 min"
["distancia"]=>
string(7) "12,1 km"
}
[3]=>
array(9) {
["colonia"]=>
string(24) "General Ignacio Zaragoza"
["municipio"]=>
string(19) "Venustiano Carranza"
["id"]=>
string(2) "27"
["gps_lat"]=>
string(9) "19.413032"
["gps_lon"]=>
string(10) "-99.096151"
["distance"]=>
string(17) "7.806943595818361"
["Nombre"]=>
string(21) "EDUARDO NUCHE CABRERA"
["tiempo"]=>
string(6) "16 min"
["distancia"]=>
string(6) "9,9 km"
}
I need to order by time (MyMultidimensionalArray["categories"][i]["tiempo"])
then by distance (MyMultidimensionalArray["categories"][i]["distancia"])
can you give some advise on how to accomplish that?
Note: The array is dynamic, it comes from a MysQl query and then i get distances and times using google maps API.
Edit 1: Thanks to Ghost for give some info, i applied the usort as:
function build_sorter($clave) {
return function ($a, $b) use ($clave) {
return strnatcmp($a[$clave], $b[$clave]);
};
}
usort($resultado["categories"], build_sorter('tiempo'));
now my array is ordered by 'tiempo' values but, when 'tiempo' values are equals i need to sort first the lower distance ('distancia'), there is the updated array order using usort:
[1]=>
array(9) {
["colonia"]=>
string(14) "Portales Norte"
["municipio"]=>
string(13) "Benito Ju�rez"
["id"]=>
string(2) "31"
["gps_lat"]=>
string(9) "19.372025"
["gps_lon"]=>
string(10) "-99.154609"
["distance"]=>
string(17) "4.000031839339196"
["Nombre"]=>
string(22) "ANDRES HERNANDEZ ORT�Z"
["tiempo"]=>
string(6) "15 min"
["distancia"]=>
string(6) "5,2 km"
}
[2]=>
array(9) {
["colonia"]=>
string(12) "Portales Sur"
["municipio"]=>
string(13) "Benito Ju�rez"
["id"]=>
string(2) "39"
["gps_lat"]=>
string(7) "19.3678"
["gps_lon"]=>
string(8) "-99.1556"
["distance"]=>
string(18) "3.7965968416905254"
["Nombre"]=>
string(25) "JOS� CARLOS ZACAULA L�PEZ"
["tiempo"]=>
string(6) "15 min"
["distancia"]=>
string(6) "4,8 km"
Is a little difficult to me to understand all functions sugested by Ghost, can you give me an example using my case?