PHP Custom Sorting
Date: 05/02/05
(PHP Community) Keywords: no keywords
If I have an array that looks like this:
$foo = array(
'mcdonalds' => array('id'=>1,'distance'=>'far','short'=>'mcds'),
'burger_king' => array('id'=>2,'distance'=>'close','short'=>'bkng'),
'subway' => array('id'=>3,'distance'=>'close','short'=>'sbwy'),
'dairy_queen' => array('id'=>4,'distance'=>'far','short'=>'dqun'),
'home' => array('id'=>5,'distance'=>'far','short'=>'home'),
)
What's the easiest/best way to sort that information based on the distance? Currently I'm rewriting that as an complext array as a simple hash that looks like name=>distance, and then I use asort to sort it accordingly. Then I re_build the original array in the correct order given the sorted simple array. It's like three steps to get that array sorted.
I looked at doing uksort and creating my own search function but it looks like uksort only takes array keys. I want it to pass the hash and then allow me to sort on $hash['distance']. Anyone done anything like this? There has to be a way with uksort to get it to handle more complex data.
Source: http://www.livejournal.com/community/php/294036.html