Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/105771-sort-an-array/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.