排序多维数组php的顺序

时间:2016-11-25 02:17:13

标签: php arrays sorting multidimensional-array usort

我尝试使用PHP array_multisort对多维数组进行排序但没有成功。 这是我的多维数组

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"
    }

我需要按时间排序( MyMultidimensionalArray [" categories"] [i] [" tiempo"]

然后按距离( MyMultidimensionalArray [" categories"] [i] [" distancia"]

你能就如何做到这一点给出一些建议吗?

注意:数组是动态的,它来自MysQl查询,然后我使用谷歌地图API获得距离和时间。

编辑1: 感谢Ghost提供了一些信息,我将usort应用为:

function build_sorter($clave) {
    return function ($a, $b) use ($clave) {
        return strnatcmp($a[$clave], $b[$clave]);
    };
}
usort($resultado["categories"], build_sorter('tiempo'));

现在我的阵列是由' tiempo'但是,当' tiempo'值是等于我需要首先排序较低的距离(' distancia'),使用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"

让我理解Ghost消化的所有功能有点困难,你能用我的案例给我一个例子吗?

0 个答案:

没有答案