sorting an array [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

sorting an array [SOLVED]

Post 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?
Last edited by s.dot on Wed Aug 24, 2005 5:12 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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);
Last edited by shiznatix on Wed Aug 24, 2005 4:53 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

shiznatix wrote:ksort()
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.
Last edited by s.dot on Wed Aug 24, 2005 4:54 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

sorry, i originally read your post wrong - i updated my first post as you where posting that reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

he means you should do this...read what he wrote :wink:

Code: Select all

// print original array 
print_r($zips); 

// print sorted array 
asort($zips, SORT_NUMERIC); 
echo '<br>'; 
print_r($zips);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Ah, either he edited or I read it wrong :P 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

scrotaye wrote:Would be looped as 1,3,2 correct?
yes
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

correct

edit: bastard pimptastic, literally one second before me :D
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

shiznatix: are you just following me and repeating what i say LOL :lol:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Awesome guys. I've spent a while on this one.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Post Reply