2017-12-05 1 views
0

Ich habe ein mehrdimensionales Array und will, dass es nach der Entfernung sortieren:Multisort nach Entfernung

[clustermarkers] => [ 
      0 => [ 
        0 => [ 
         'name' => 'A', 
         'distance' => 10 
        ] 
      ], 
      1 => [ 
        0 => [ 
         'name' => 'B', 
         'distance' => 8 
        ] 
      ], 
      ... 
]; 

Ich habe versucht, usort Funktion, aber etwas nicht in Ordnung:

usort($clustermarkers, function($a, $b) { 
    return (int)$a['distance'] - (int)$b['distance']; 
}); 

Antwort

1

In Ihrer usort Funktion, fügen Sie einfach [0]-$a und $b vor ['distance']

usort($clustermarkers, function ($a, $b) { 
    return $a[0]['distance'] - $b[0]['distance']; 
}); 

print_r($clustermarkers); 
0

Fast:

usort($clustermarkers, function($a, $b) { 
    return $a['distance'] > $b['distance']; //Distance ASC 
}); 

usort($clustermarkers, function($a, $b) { 
    return $a['distance'] < $b['distance']; //Distance DESC 
}); 

beachten Sie die < und > Betreiber und nicht Gießen zu int

+0

Notice: Undefined index: Abstand – user889349

Verwandte Themen