Barrucadu Established Member ★ 20 ★ Impact 64 Sep 1, 2007 1K views 8 replies #1 Sorting an array. I've got an array in this format: PHP: $array['text'] = number; How would I sort by the numbers, not the text? I've tried: PHP: $tmp = array(); foreach($array as $key => $var){ $tmp[$var] = $key; } $array = ksort($tmp); But it dosn't work.
Sorting an array. I've got an array in this format: PHP: $array['text'] = number; How would I sort by the numbers, not the text? I've tried: PHP: $tmp = array(); foreach($array as $key => $var){ $tmp[$var] = $key; } $array = ksort($tmp); But it dosn't work.
Peter VIP Member VIP ★ 20 ★ Impact 209 Sep 1, 2007 #2 The following demonstrated how to do it. I have used print_r to show the order before and after the sorting. PHP: <?php $array = array('one'=>'1','three'=>'3','seven'=>'7','two'=>'2'); print_r($array); asort($array); print_r($array); ?> The method of using ksort that you was using will not work in ordering the values in the array as ksort is used for ordering the key (where you have the text) ksort Last edited: Sep 1, 2007
The following demonstrated how to do it. I have used print_r to show the order before and after the sorting. PHP: <?php $array = array('one'=>'1','three'=>'3','seven'=>'7','two'=>'2'); print_r($array); asort($array); print_r($array); ?> The method of using ksort that you was using will not work in ordering the values in the array as ksort is used for ordering the key (where you have the text) ksort
Barrucadu Established Member ★ 20 ★ Impact 64 Sep 1, 2007 #3 Nevermind, stupid mistake. Last edited: Sep 1, 2007
Peter VIP Member VIP ★ 20 ★ Impact 209 Sep 1, 2007 #4 yeah I see what you have done. The asort returns 1 because it was successful in sorting the array.
Barrucadu Established Member ★ 20 ★ Impact 64 Sep 1, 2007 #5 Still not working PHP: echo "<pre>UNSORTED:\r\n"; print_r($thing); echo "\r\nSORTED:\r\n"; asort($thing, SORT_NUMERIC); print_r($thing); echo "</pre>"; Outputs: Code: <pre>UNSORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) SORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) </pre>
Still not working PHP: echo "<pre>UNSORTED:\r\n"; print_r($thing); echo "\r\nSORTED:\r\n"; asort($thing, SORT_NUMERIC); print_r($thing); echo "</pre>"; Outputs: Code: <pre>UNSORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) SORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) </pre>
Peter VIP Member VIP ★ 20 ★ Impact 209 Sep 1, 2007 #6 can you post the full code and the expected output. Looking at what you just posted I might be misunderstanding what you want. The array is already sorted by the values.
can you post the full code and the expected output. Looking at what you just posted I might be misunderstanding what you want. The array is already sorted by the values.
mvl fka: leonardoEstablished Member ★ 15 ★ Impact 72 Sep 1, 2007 #7 Mikor said: Still not working PHP: echo "<pre>UNSORTED:\r\n"; print_r($thing); echo "\r\nSORTED:\r\n"; asort($thing, SORT_NUMERIC); print_r($thing); echo "</pre>"; Outputs: Code: <pre>UNSORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) SORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) </pre> Click to expand... The array looks sorted to me, although it already was sorted before the asort. How did expect it to look?
Mikor said: Still not working PHP: echo "<pre>UNSORTED:\r\n"; print_r($thing); echo "\r\nSORTED:\r\n"; asort($thing, SORT_NUMERIC); print_r($thing); echo "</pre>"; Outputs: Code: <pre>UNSORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) SORTED: Array ( [yahoo] => 4 [opera 9.22] => 9 [internet explorer 7.0] => 12 [internet explorer 6.0] => 29 ) </pre> Click to expand... The array looks sorted to me, although it already was sorted before the asort. How did expect it to look?
Barrucadu Established Member ★ 20 ★ Impact 64 Sep 1, 2007 #8 Got it working now, I needed to use arsort. (The fact that it was sorted before was a coincidence, it probably won't always be)
Got it working now, I needed to use arsort. (The fact that it was sorted before was a coincidence, it probably won't always be)
Peter VIP Member VIP ★ 20 ★ Impact 209 Sep 1, 2007 #9 ahh so you wanted the numbers in reverse order i.e. highest first.