Andy-H Posted February 7, 2012 Share Posted February 7, 2012 Does anyone know of a way I can natsort a multi-dimension array by a value nested in the array 2 keys deep? I.e. array( 'test1' => array( 'location' => '123 fake street', 'time' => 120 ), 'test2' => array( 'location' => '123 elm street', 'time' => 34 ), 'test3' => array( 'location' => '123 php street', 'time' => 133 ) ); // would become array( 'test2' => array( 'location' => '123 elm street', 'time' => 34 ), 'test1' => array( 'location' => '123 fake street', 'time' => 120 ), 'test3' => array( 'location' => '123 php street', 'time' => 133 ) ); thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/256606-natsort-multi-dimensional-array/ Share on other sites More sharing options...
digibucc Posted February 7, 2012 Share Posted February 7, 2012 i've never used natsort, but i know array_multisort can sort inside... might help? http://php.net/manual/en/function.array-multisort.php Quote Link to comment https://forums.phpfreaks.com/topic/256606-natsort-multi-dimensional-array/#findComment-1315463 Share on other sites More sharing options...
ManiacDan Posted February 7, 2012 Share Posted February 7, 2012 You can also just use usort Quote Link to comment https://forums.phpfreaks.com/topic/256606-natsort-multi-dimensional-array/#findComment-1315464 Share on other sites More sharing options...
Andy-H Posted February 7, 2012 Author Share Posted February 7, 2012 Cheers, thats what I'm lloking at ATM Dan, do you know the function used for natsorting in PHP?? usort($info['summary'], function($v1, $v2) { return strnatcmp($v1, $v2); }); That doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/256606-natsort-multi-dimensional-array/#findComment-1315465 Share on other sites More sharing options...
Andy-H Posted February 7, 2012 Author Share Posted February 7, 2012 usort($info['summary'], function($v1, $v2) { return (strtolower($v1['time']) == strtolower($v2['time'])) ? strnatcmp($v1['time'], $v2['time']) : strnatcasecmp($v1['time'], $v2['time']); }); Got it, cheers guys (Y) Quote Link to comment https://forums.phpfreaks.com/topic/256606-natsort-multi-dimensional-array/#findComment-1315468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.