tastro Posted September 21, 2011 Share Posted September 21, 2011 how to natsort() by key and not by value? because ksort and krsort don't work the way i need them to work. Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/ Share on other sites More sharing options...
tastro Posted September 21, 2011 Author Share Posted September 21, 2011 i found a code already on the web. but i don't understand all of it. also if someone could explain to me why is there an & in the function here: knatsort(&$array) before $array ? and why is there an return true; at the end? <?php function knatsort(&$array){ $array_keys=array_keys($array); natsort($array_keys); $new_natsorted_array=array(); $array_keys_2=''; foreach($array_keys as $array_keys_2){ $new_natsorted_array[$array_keys_2]=$array[$array_keys_2]; } $array=$new_natsorted_array; return true; } knatsort($top_rated_array_with_votes_as_keys); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271342 Share on other sites More sharing options...
Adam Posted September 21, 2011 Share Posted September 21, 2011 You could just use uksort in combination with natsort: uksort($array, 'natsort'); Just to explain the use of "&" though, it means pass the array by reference. Normally when you use the sorting functions, you don't assign the return value back, you just call it and the array passed in the parameters is updated. If the "&" wasn't included you would need to return the array within the function and call it like: $returned_array = knatsort($passed_array); You don't need to go to these lengths though. Just use the snippet I provided above. Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271347 Share on other sites More sharing options...
Andy-H Posted September 21, 2011 Share Posted September 21, 2011 <?php $array = array('a9' => 0,'a' => 1,'a2' => 2,'b6' => 3,'b1' => 4); echo '<pre>' . print_r($array, 1) . '</pre>'; $array = array_flip($array); natsort($array); $array = array_flip($array); echo '<pre>' . print_r($array, 1) . '</pre>'; ?> Array ( [a9] => 0 [a] => 1 [a2] => 2 [b6] => 3 [b1] => 4 ) Array ( [a] => 1 [a2] => 2 [a9] => 0 [b1] => 4 [b6] => 3 ) Works but will remove any multiple values ( effectively array_unique )@Tastro[pre]The & indicates that the variable should be "passed by reference", this means that instead of copying the variables value into another variable within the functions scope "passing by value", the function [/size]receives a pointer to the value's memory address ( where it is stored in RAM ) and the value is updated in the scope from which it was called. This means that any changes made to the value within the function scope, take effect on the variable outside of the function.[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271349 Share on other sites More sharing options...
tastro Posted September 21, 2011 Author Share Posted September 21, 2011 You could just use uksort in combination with natsort: uksort($array, 'natsort'); Just to explain the use of "&" though, it means pass the array by reference. Normally when you use the sorting functions, you don't assign the return value back, you just call it and the array passed in the parameters is updated. If the "&" wasn't included you would need to return the array within the function and call it like: $returned_array = knatsort($passed_array); You don't need to go to these lengths though. Just use the snippet I provided above. thank you very much for your explanation, it's all clear now. i always use this methode: $returned_array = knatsort($passed_array); that's why i didn't know how the & and return true works. and thank you for this function: uksort($array, 'natsort'); it's really easier to work with such a small piece of code. topic solved. Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271353 Share on other sites More sharing options...
tastro Posted September 21, 2011 Author Share Posted September 21, 2011 topic not solved yet. this one doesn't work the way i need it. uksort($array,'natsort'); it doesn't sort by the keys. it sorts still by values. Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271355 Share on other sites More sharing options...
Adam Posted September 21, 2011 Share Posted September 21, 2011 $array = array( 3 => 1, 2 => 2, 1 => 3 ); uksort($array, 'natsort'); print_r($array); /* Result: Array ( [1] => 3 [2] => 2 [3] => 1 ) */ Works as expected for me. Can you show the way you're using it? Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271357 Share on other sites More sharing options...
tastro Posted September 21, 2011 Author Share Posted September 21, 2011 1. with your code, i get this error: "Warning: natsort() expects exactly 1 parameter, 2 given" 2. i tryed it like this: <?php $array = array( 22 => 22, 1 => 1, 13 => 13 ); uksort($array, 'natsort'); print_r($array); ?> but it doesn't work. i get this: Array ( [13] => 13 [1] => 1 [22] => 22 ) but it should be like this: Array ( [1] => 1 [13] => 13 [22] => 22 ) Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1271384 Share on other sites More sharing options...
tastro Posted October 1, 2011 Author Share Posted October 1, 2011 maybe it's because i get an error, this one: "Warning: natsort() expects exactly 1 parameter, 2 given" but i don't know what's wrong and how to fix it. :S this is my code: <?php $array = array( 22 => 22, 1 => 1, 13 => 13 ); uksort($array,'natsort'); print_r($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1274551 Share on other sites More sharing options...
tastro Posted October 1, 2011 Author Share Posted October 1, 2011 lol, i don't know how i missed your post andy, but your code works like a charm! thank you big time! also this one works perfect! <?php $array=array('22'=>'22','1'=>'1','13'=>'13'); $array=array_flip($array); natsort($array); $array=array_flip($array); print_r($array); ?> <?php $array = array('a9' => 0,'a' => 1,'a2' => 2,'b6' => 3,'b1' => 4); echo '<pre>' . print_r($array, 1) . '</pre>'; $array = array_flip($array); natsort($array); $array = array_flip($array); echo '<pre>' . print_r($array, 1) . '</pre>'; ?> Array ( [a9] => 0 [a] => 1 [a2] => 2 [b6] => 3 [b1] => 4 ) Array ( [a] => 1 [a2] => 2 [a9] => 0 [b1] => 4 [b6] => 3 ) Works but will remove any multiple values ( effectively array_unique )@Tastro[pre]The & indicates that the variable should be "passed by reference", this means that instead of copying the variables value into another variable within the functions scope "passing by value", the function [/size]receives a pointer to the value's memory address ( where it is stored in RAM ) and the value is updated in the scope from which it was called. This means that any changes made to the value within the function scope, take effect on the variable outside of the function.[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/247579-how-to-natsort-by-key-and-not-by-value/#findComment-1274553 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.