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!

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 Loops Todo App

miguel catt
miguel catt
1,424 Points

Can 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

Hi Miguel,

The php sort() function orders the array to a low to high order ( example 1-10 )

miguel catt
miguel catt
1,424 Points

Hi 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.

Hi 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.

<?php
echo '<pre>';
var_dump($sort);
echo '</pre>';
miguel catt
miguel catt
1,424 Points

Thanks Mark! Now it makes sense!

Anytime! Glad I was able to help

Christopher Parke
Christopher Parke
21,978 Points

I 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.

exactly... 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
Andrew Patella
2,270 Points

Hey mark, I didn't realize that was an issue. Thanks for the advice. Will do.

Hi 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
Andrew Patella
2,270 Points

Makes sense, thanks again