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

Andrew Patella
Andrew Patella
2,270 Points

krsort() asort() returns chocolate?

I dont understand how the correlation between ksort and asort work. It seems like it should return "black cherry" or bool(true) at the top but its returning "Chocolate". Someone mentioned because we are sorting backwards by key then by value but honestly it doesn't make sense to me.

Tim Makin
Tim Makin
17,261 Points

Hi Andrew, I've just done this video and despite my best efforts I can't get my head around it either. Maybe Alena Holligan could shed some light on it...

I agree with you Andrew. I finished the video and I am also scratching my head as to why "Black Cherry" is last and "Chocolate" first when you run the code exactly as Alena Holligan did in the video (krsort followed by asort).

I'm also wondering why Alena is running different code in her reply to you, than the code she ran in her video.

2 Answers

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

asort is sorting by the VALUE while ksort is sorting by KEY ("a" for "array sort" and "k" for "key sort")

arsort is sorting by VALUE in REVERSE order while krsort is sorting by KEY in REVERSE order (Notice the letter "r" in the functions for "reverse")

Tim Makin
Tim Makin
17,261 Points

Hi Alena, thanks very much for the response :) I'm still a bit confused though...

If we run krsort on the $iceCream array we get:

array(7) {
  ["alena"]=>
  string(9) "Pistachio"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["Andrew"]=>
  bool(true)
  ["Alena"]=>
  string(12) "Black Cherry"
}

So the key "alena" is the first element of the array, which I get because it's lowercase so comes before the uppercase keys.

If we run asort on the $iceCream array we get:

array(7) {
  ["Alena"]=>
  string(12) "Black Cherry"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["alena"]=>
  string(9) "Pistachio"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Andrew"]=>
  bool(true)
}

So the value "Black Cherry" comes first if sorting on array values.

But if we run krsort followed by asort we get the following result:

array(7) {
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["alena"]=>
  string(9) "Pistachio"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Andrew"]=>
  bool(true)
  ["Alena"]=>
  string(12) "Black Cherry"
}

So now the array value "Chocolate" is coming first, surely this should be "Black Cherry" as when asort was run independently? I don't get what affect running krsort is having on the asort that's being performed after.

Sorry if this is something really simple that I'm missing. I have noticed that there is a warning in the PHP documentation for the sort() function to "be careful when sorting arrays with mixed types values because sort() can produce unpredictable results."

I know in this case we are using asort() but when I removed the element that holds the boolean value the sort worked as I expected with "Black Cherry" at the top so maybe is something to do with this?

Thanks again for your input :)

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

So here is my code

$iceCream = array(
    'Alena' => 'Black Cherry', 
    'Treasure' => 'Chocolate', 
    'Dave McFarland' => 'Cookies and Cream', 
    'Rialla' => 'Strawberry'
);
$iceCream['alena'] = 'Pistachio';
$iceCream['Dave Thomas'] = 'Cookies and Cream';
$iceCream[] = 'Vanilla';
$iceCream['Andrew'] = true;
krsort($iceCream);
var_dump($iceCream);
asort($iceCream);
var_dump($iceCream);
krsort($iceCream);
var_dump($iceCream);

And here are my results

array(8) {
  ["alena"]=>
  string(9) "Pistachio"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["Alena"]=>
  string(12) "Black Cherry"
  [0]=>
  string(7) "Vanilla"
  ["Andrew"]=>
  bool(true)
}
array(8) {
  ["Alena"]=>
  string(12) "Black Cherry"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["alena"]=>
  string(9) "Pistachio"
  ["Rialla"]=>
  string(10) "Strawberry"
  [0]=>
  string(7) "Vanilla"
  ["Andrew"]=>
  bool(true)
}
array(8) {
  ["alena"]=>
  string(9) "Pistachio"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["Alena"]=>
  string(12) "Black Cherry"
  [0]=>
  string(7) "Vanilla"
  ["Andrew"]=>
  bool(true)
}

Both krsort produce the same results, they are sorting by key in reverse order. Capital letters come first so A-Z then a-z, "alena" being the last element.

asort sorts my array by value, thus producing "Black Cherry" first. How are you running krsort then asort?

Tim Makin
Tim Makin
17,261 Points

Hi Alena, thanks again for your reply - really appreciate you taking the time to respond.

I've ran your code and get the same results as you but following along with the video we actually have one less element in the array. At 4:29 you note that ksort with not work if numeric and string keys are mixed together and then comment out the line:

//$iceCream[] = 'Vanilla';

At 5:24 in the video you perform krsort then asort on the array and receive the same output (with Chocolate being first, not Black Cherry) as I have been getting.

I also get the same results if I take the code you posted above and comment out the $iceCream[] = 'Vanilla' line.

So my test code:

$iceCream = array(
    'Alena' => 'Black Cherry', 
    'Treasure' => 'Chocolate', 
    'Dave McFarland' => 'Cookies and Cream', 
    'Rialla' => 'Strawberry'
);
$iceCream['alena'] = 'Pistachio';
$iceCream['Dave Thomas'] = 'Cookies and Cream';
//$iceCream[] = 'Vanilla';
$iceCream['Andrew'] = true;
krsort($iceCream);
var_dump($iceCream);
asort($iceCream);
var_dump($iceCream);
krsort($iceCream);
var_dump($iceCream);

Produces:

array(7) {
  ["alena"]=>
  string(9) "Pistachio"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["Andrew"]=>
  bool(true)
  ["Alena"]=>
  string(12) "Black Cherry"
}
array(7) {
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["alena"]=>
  string(9) "Pistachio"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Andrew"]=>
  bool(true)
  ["Alena"]=>
  string(12) "Black Cherry"
}
array(7) {
  ["alena"]=>
  string(9) "Pistachio"
  ["Treasure"]=>
  string(9) "Chocolate"
  ["Rialla"]=>
  string(10) "Strawberry"
  ["Dave Thomas"]=>
  string(17) "Cookies and Cream"
  ["Dave McFarland"]=>
  string(17) "Cookies and Cream"
  ["Andrew"]=>
  bool(true)
  ["Alena"]=>
  string(12) "Black Cherry"
}