waveol Posted May 15, 2008 Share Posted May 15, 2008 Hi, I've got a problem with sorting. The sort() function sorts an array by the values. This function assigns new keys for the elements in the array. Existing keys will be removed. But I need to keep the key as a string. I've got the code: <?php // print choices for functions print("<b>Pick a function:</b><br />"); print("<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">"); ?> <p> <input type="radio" name="func" value="asort"></input>asort<br /> <input type="radio" name="func" value="arsort"></input>arsort<br /> <input type="radio" name="func" value="ksort"></input>ksort<br /> <input type="radio" name="func" value="sort"></input>sort<br /> </p> <p> <input type="submit" name="set" value="Do it!"></input> </p> </form> <?php if (!empty($_POST['func'])) { $arr_words = array( "fruit" => "cherry", "fish" => "flounder", "Animal" => "bear", "TLA" => "FYI", "Number" => 25); switch ($_POST['func']) { case "asort": asort($arr_words); print("Function: <b>asort()</b> -- Sort an array and maintain index association<br />\n"); break; case "sort": sort($arr_words); print("Function: <b>sort()</b> -- Sort an array and maintain index association<br />\n"); break; case "arsort": arsort($arr_words); print("Function: <b>arsort()</b> -- Sort an array in reverse order and maintain index association<br />\n"); break; case "ksort": ksort($arr_words); print("Function: <b>ksort()</b> -- Sort an array by key<br />\n"); break; } // print array to screen print("Current contents of \$arr_words:<p>\n"); print("<table><tr><td><b>KEY</b></td><td><b>VALUE</b></td></tr>\n"); reset($arr_words); while (list ($key, $val) = each ($arr_words)) { print("<tr><td>$key</td><td>$val</td></tr>\n"); } print($_POST); print("</table></p>"); } ?> In the manual mentioned that - Learners have experienced the problem of properly displaying the form data in subsequent pages after a sort() function is invoked. This problem can be avoided by using hidden fields as shown below. <input type="hidden" name="array[fruit]" value="<?php print($arr_words["fruit"]); ?>" /> <input type="hidden" name="array[fish]" value="<?php print($arr_words["fish"]); ?>" /> I really need help with it, still can't figure out how to do it and how to get the sorting result something as: KEY VALUE Animal bear Number 25 TLA FYI fish flounder fruit cherry and still using SORT() and the HIDDEN option. Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/105771-sort-an-array/ Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 use asort() instead of sort() Quote Link to comment https://forums.phpfreaks.com/topic/105771-sort-an-array/#findComment-542011 Share on other sites More sharing options...
darkfreaks Posted May 15, 2008 Share Posted May 15, 2008 asort ()or usort ()would work i think ??? Quote Link to comment https://forums.phpfreaks.com/topic/105771-sort-an-array/#findComment-542019 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 usort is user-defined, but your still loss the keys (i think) if you wanted it to be user-defined then your need to use uasort but asort() is the one you want Quote Link to comment https://forums.phpfreaks.com/topic/105771-sort-an-array/#findComment-542033 Share on other sites More sharing options...
waveol Posted May 15, 2008 Author Share Posted May 15, 2008 usort is user-defined, but your still loss the keys (i think) if you wanted it to be user-defined then your need to use uasort but asort() is the one you want Thanks for paying attention to my post. Quote Link to comment https://forums.phpfreaks.com/topic/105771-sort-an-array/#findComment-542145 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 well if thats not what your asking then just say so.. your question is confusing, (it hardly seams like you even asking one) never mine happy coding Quote Link to comment https://forums.phpfreaks.com/topic/105771-sort-an-array/#findComment-542161 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.