Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Arrays and Control Structures PHP Arrays Sorting Arrays

Sam Gord
Sam Gord
14,084 Points

asort / arsort

it seems that i cant get the difference between using asort/arsort in associative arrays and indexed arrays , no matter how many times i replay this video! can anyone please help? thank you in advance ;)

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Sam,

It just depends on whether you want the results to be sorted in Ascending or Descending order. Just remember the the "r" in arsort() stands for "Reverse"... so, it sorts in reverse order.

asort() sorts in Ascending
arsort() sorts in Descending

For example: (2, 6, 1, 7, 4, 3)

asort() will return 1, 2, 3, 4, 6, 7
arsort() wil return 7, 6, 4, 3, 2, 1

Hope that helps! :) :dizzy:

Sam Gord
Sam Gord
14,084 Points

thanks very much

So what is the difference between arsort() and rsort()?