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 trialmiguel catt
1,424 PointsCan somebody explain this to me? I'm getting confused
Can somebody explain whats happening with the Sort in this part of the code?
I couldn't understand what the instructor was saying in the video.
Maybe a different explanation can help me with this.
<?php
include 'list.php';
$status = 'all';
$field = 'priority';
//Checks all complete/incomplete items in list
$order = array();
if($status == 'all') {
$order = array_keys($list);
} else {
foreach($list as $key => $item) {
if($item['complete'] == $status){
$order[] = $key;
}
}
}
//Sorts list
if($field) {
$sort = array();
foreach($order as $id) {
$sort[$id] = $list[$id][$field];
}
}
?>
3 Answers
Mark Truitt
17,230 PointsHi Miguel,
The php sort() function orders the array to a low to high order ( example 1-10 )
Christopher Parke
21,978 PointsI hate how she says "And then you do this, and this and this" but never explains why. I'm sitting here with an app I've written that I don't understand. I expect better.
Michael Johnson
6,340 Pointsexactly... ive managed to follow along fairly well up until the last 2 videos... ive watched them at least 20 times and still dont understand.
Andrew Patella
2,270 PointsHey mark, I didn't realize that was an issue. Thanks for the advice. Will do.
Mark Truitt
17,230 PointsHi Andrew,
Not really an issue per-say but, most people when looking for help through the community will look at the name then stop at the answer flagged as correct. If someone else has the same question it would be easier and quicker for them to get that help.
Andrew Patella
2,270 PointsMakes sense, thanks again
miguel catt
1,424 Pointsmiguel catt
1,424 PointsHi Mark,
Thanks for that answer but what I meant was the $id variable in the foreach loop is throwing me off. It is used in $sort and $list. Which is confusing me.
Sorry about that. I should have made the question more specific.
Mark Truitt
17,230 PointsMark Truitt
17,230 PointsHi Miguel,
What she did was set it up so that the key in the sort array matches the key in the list array. This is so that when it runs through 0(Sort) 0(List), 1(Sort) 1(List), 2(Sort) 2(List) and then the value in the sort array matches the priority in the list array.
You can also wrap var_dump like this to avoid having to stare at it in the source code.
miguel catt
1,424 Pointsmiguel catt
1,424 PointsThanks Mark! Now it makes sense!
Mark Truitt
17,230 PointsMark Truitt
17,230 PointsAnytime! Glad I was able to help