Page 1 of 2
sorting an array [SOLVED]
Posted: Wed Aug 24, 2005 4:45 am
by s.dot
I currently have an array returned from a form that is in the following sequence:
Code: Select all
Array ( [46928] => 1.32 [46930] => 8.34 [46933] => 3.19 [46938] => 5.1 [46953] => 1.47 [46987] => 7.44 [46989] => 4.45 [46991] => 9.29 )
I need to sort these values by the value ASC, maintaining the array keys
I tried to use:
Code: Select all
$sorted = asort($zips, SORT_NUMERIC);
but apparently I got the syntax wrong, as print_r shows up empty.
How would I go about doing this?
Posted: Wed Aug 24, 2005 4:50 am
by shiznatix
asort() only returns true or false so you cant assign a variable to this just do
Code: Select all
$newarr = $oldarr;
asort($newarr, SORT_NUMERIC);
print_r($newarr);
Posted: Wed Aug 24, 2005 4:52 am
by s.dot
I don't need to sort by keys, I need to sort by the value of the key (the numbers in my first post) while maintaining the keys.
I believe asort() would work fine if I could get it to sort numerically.
Posted: Wed Aug 24, 2005 4:53 am
by shiznatix
sorry, i originally read your post wrong - i updated my first post as you where posting that reply
Posted: Wed Aug 24, 2005 5:00 am
by s.dot
That is what I thought as well, but the print_r is returning 1
Code:
Code: Select all
// print original array
print_r($zips);
// print sorted array
$newarray = asort($zips, SORT_NUMERIC);
echo '<br>';
print_r($newarray);
The result is:
Code: Select all
Array ( [46928] => 1.32 [46930] => 8.34 [46933] => 3.19 [46938] => 5.1 [46953] => 1.47 [46987] => 7.44 [46989] => 4.45 [46991] => 9.29 )
1
Posted: Wed Aug 24, 2005 5:01 am
by JayBird
he means you should do this...read what he wrote
Code: Select all
// print original array
print_r($zips);
// print sorted array
asort($zips, SORT_NUMERIC);
echo '<br>';
print_r($zips);
Posted: Wed Aug 24, 2005 5:05 am
by shiznatix
yes you can not assign a variable to asort() becuase it does not return a sorted array it just returns true or false
Returns TRUE on success or FALSE on failure.
so you just do it and it will work for you
Posted: Wed Aug 24, 2005 5:07 am
by s.dot
Ah, either he edited or I read it wrong

Needless to say, I've now got it working.
Quick off-topic question. foreach() starts at the first array element and goes through the loop in order from first to last, correct?
For example
[a] => 1
[d] => 3
[c] => 2
Would be looped as 1,3,2 correct? (and not 1,2,3 or 3,2,1)
Posted: Wed Aug 24, 2005 5:08 am
by JayBird
scrotaye wrote:Would be looped as 1,3,2 correct?
yes
Posted: Wed Aug 24, 2005 5:08 am
by shiznatix
correct
edit: bastard pimptastic, literally one second before me

Posted: Wed Aug 24, 2005 5:09 am
by JayBird
shiznatix: are you just following me and repeating what i say LOL

Posted: Wed Aug 24, 2005 5:12 am
by s.dot
Awesome guys. I've spent a while on this one.
Posted: Wed Aug 24, 2005 5:13 am
by Chris Corbyn
If you want to maintain the original array and end up with a sorted copy just copy it to a new duplicate array first and then sort that

Posted: Wed Aug 24, 2005 5:15 am
by raghavan20
Code: Select all
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);//flip keys and values
asort($trans);//sort based on keys now
$trans = array_flip($trans);//flip back again
print_r($trans);
Posted: Wed Aug 24, 2005 5:16 am
by Chris Corbyn
raghavan20 wrote:Code: Select all
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);//flip keys and values
asort($trans);//sort based on keys now
$trans = array_flip($trans);//flip back again
print_r($trans);
ksort() hint hint
