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 trialrobbertdekock
13,390 Pointsasort($iceCream); doesn't seem to output the expected result
In the final video of sorting arrays, the teachers sorts $iceCream by using asort($iceCream); to see which person likes Chocolate ice cream the most. asort() sorts by the value of an array, from low to high, so you would expect that 'Black Cherry' pops up as first, but it doesn't. It's probably something very easy, but I don't seem te understand it. Can somebody help me please? the code in the video is as followed.
<?php
$iceCream = array(
'Alena' => 'Black Cherry',
'Treasure' => 'Chocolate',
'Dave McFarland' => 'Cookies and Cream',
'Rialla' => 'Strawberry',
);
$iceCream['alena'] = 'Pistachio';
$iceCream['Dave Thomas'] = 'Cookies and Cream';
$iceCream['Andrew'] = true;
krsort($iceCream);
asort($iceCream);
var_dump($iceCream);
?>
Thank you in advance!
robbertdekock
13,390 PointsHi Ashish, thank you for your response. The thing is. My code is exactly the same as the teachers code, my output is exactly the same as my teachers' output, but I don't understand the output. Why does sortation starts with 'Chocolate'?
6 Answers
Andrew Patella
2,270 PointsOK I think I figured this out.
And Robert you are not ignorant I'm sure many who watched this video didn't get it either, and that's because you can't sort an array twice with consistent results. as of php version 4.1.0
Im adding a link to php.net that explains this. https://bugs.php.net/bug.php?id=53341&edit=1
anyway this particular use of the sort method isn't used in the real world. not sure why it was even mentioned. Hope that helps
Jennifer Nordell
Treehouse TeacherHi there! It's because you're doing the krsort on the array first which sets the array in reverse order by the key. The keys are the names of the people. The values are the flavors. So Treasure's key is going to end up first, as her name would come last alphabetically and we're sorting in reverse. And the value for her ice cream is "Chocolate". I encourage you to comment out the krsort and run it again. If you do, you'll see Black Cherry at the top. with Alena as the key.
Hope this helps!
robbertdekock
13,390 PointsHi Jennifer,
After trying the steps you explain, I still can't see why this results return. You say that krsort sorts by key in reverse order, which is correct. However, this formule puts capitals above normal letters, which results in 'alena' at the top of the var_dump output. You're probably right, but I can't stand the fact that I don't seem to understand this... ;)
Sorry for my ignorance and thank you for your time and effort.
Kind regards,
Robbert
Markus Mönch
16,383 Pointsi also dont understand this. teamtreehouse is very bad because they dont help here.
James Ackerman
14,099 PointsI agree. This is really confusing. Regardless of whether I comment out the ksort() function, I get the same result with "Alena" => "Black Cherry" at the top of the results.
Seth Johnson
15,200 PointsI don't know if this helps anyone make sense of these sorting methods, but one very crucial aspect of them -- that to the best of my knowledge wasn't mentioned anywhere in this tutorial -- is that they're also case sensitive, as well as format sensitive, to an extent; my practice scripts had a variety of upper and lower casing contained in my array values, as well as blank spaces that I deliberately put into some of my strings, and it took me awhile to figure out that both of those things affect these sorting methods.
Ashish Mehra
432 PointsAshish Mehra
432 Pointsrobbertdekock , I think you make some typo mistake in function i.e. krsort instead of ksort.