user3578082
user3578082

Reputation:

Why would arsort() make a central value as the first?

While learning PHP at teamtreehouse.com I came across this question:

$colors = array("Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Black");

Which function will sort the array so the first element is "Yellow"?

The answer to the question is arsort(), but I can't understand why, because the PHP man says:

arsort — Sort an array in reverse order and maintain index association

I understand from the man that arsort() will just reverse the order, so that Black will become first instead of last, but then, why does the Treehouse question answer says Yellow will be first?

Might it be a authors mistake in the question? Thanks.

Note: I tried to create a headline useful for people with similar confusion.

Upvotes: 4

Views: 100

Answers (1)

Alexander O'Mara
Alexander O'Mara

Reputation: 60577

Both asort and arsort sort the input array (alphabetically in this case), it's just a matter of which order then are sorted in.

asort — Sort an array and maintain index association

arsort — Sort an array in reverse order and maintain index association

So it's not just a simple array reversal like array_reverse, it sorts them too.

Upvotes: 4

Related Questions