PHP - How To Natsort() By Key And Not By Value?
how to natsort() by key and not by value?
because ksort and krsort don't work the way i need them to work. Similar TutorialsLet's say I have an array: $myArray[0] = "AtG2"; $myArray[1] = "AtG4"; $myArray[2] = "AtG1"; $myArray[3] = "AtG3"; $myArray[4] = "AtG5"; I want to sort it alphanumerically, so that the key/value pairs are as follows: [zero*] => "AtG1" [1] => "AtG2" [2] => "AtG3" [3] => "AtG4" [4] => "AtG5" * = I have to write [zero] because putting an actual zero number inside of [ and ] braces converts it to an HTML list bullet. It *appears* that natsort() is the proper function for this. The only problem I'm having is that natsort() doesn't manipulate key/value pairs. To confuse me even more, literally every example I can find on using natsort() uses it in conjunction with print_r() like so: natsort($someArray); print_r($someArray); Obviously, something is happening to $someArray when passed to natsort(), otherwise, print_r() wouldn't be able to order the indices in synch with the natural ordering algorithm. Well, I don't need my web app to use print_r()! I just need the darn key/value pairs reordered so that when I echo $myArray[3], I know I'm going to get "AtG4". What am I missing here? What is the point of calling it a "sort" if it doesn't manipulate the key/value pairs??? How can I get what I want?!? 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 |