Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - How to sort the same array multiple times?
    text
    copied!<p>I am trying to make a simple script that will first put an array() into function so I can call on it multiple times to sort. Here is what my array looks like:</p> <pre><code>// I want this inside of a function so I can call on it: $a = array( 15 =&gt; "C", 12 =&gt; "E", 11 =&gt; "B", 19 =&gt; "P", 10 =&gt; "L", 14 =&gt; "N", 20 =&gt; "A" ); // This is how I brought it into a function and formatted it: function original_array($a){ foreach($a as $key =&gt; $types) { print $key . " " . ":" . " " . $types . "&lt;br /&gt;"; } } </code></pre> <p>I then just have to call original_array() and it prints out fine, but if I sort it once I cant sort it again. It will just print out the first sort:</p> <pre><code>// Print out array is is: original_array(); // Then I print out array with sort(): sort($a); original_array($a); // But if I try and sort it again with different sort it doesn't work: ksort($a); original_array($a); </code></pre> <p>What am I doing wrong? I am somewhat new to PHP so your help is greatly appreciated. </p> <p><strong>UPDATE://</strong></p> <p>This is what I ended up doing. I should have read up on sort function a little more thoroughly. I was unaware that it destroyed the original pointers.</p> <pre><code>&lt;?php // Original array: $a = array( 15 =&gt; "C", 12 =&gt; "E", 11 =&gt; "B", 19 =&gt; "P", 10 =&gt; "L", 14 =&gt; "N", 20 =&gt; "A" ); // Array for sort() function: $b = $a function print_format($array){ foreach($array as $key =&gt; $types) { print $key . " " . "=&gt;" . " " . $types . "&lt;br /&gt;"; } } print "Original"; print_format($a); print "sort()"; sort($b); print_format($b); print "ksort()"; ksort($a); print_format($a); print "asort()"; asort($a); print_format($a); print "krsort()"; krsort($a); print_format($a); print "rsort()"; rsort($b); print_format($b); print "arsort()"; arsort($a); print_format($a); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload