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

Attempt at an explanation of a complicated point at the start of the sorting multi dimensional arrays video.

OK so the bit that was confusing me was near the start where Alena changed the expression in the filter loop if statement to $filter[$originalKey] = $item['priority']. I couldn't work out why that generated six rows in the browser displayed table but they were six rows of just three repeated items (Dishes, Dust and Vacuum) in a seemingly meaningless and random order rather all six possible items in the $list array . IF any one wants to correct anything I have wrong below please do.

The answer, I think, is because:

1) the status is set $status === 'all';

2) the $originalKey variable copies all all six keys(i.e 0,1,2,3,4,5) from the original $list array (because it is defined as such in the foreach loop (foreach( $list as $originalKey => $item).

3) the expression in the if statement sets the $filter array keys to be exactly the same as the six $list keys (0,1,2,3,4,5) by becoming the $variable name (i.e the left hand side of the expression) in the $filter[$originalKey] = $item['priority'] expression. The important thing to remember here is that keys in the $filter array are COMPLETELY SEPARATE and NEW set of keys. They are are not the keys from the $list MD array, they are just exact copies of the latter. Because they status is set to all, you will get all six keys copied into the new $filter array.

4) In much the same way, the values of the 'priority' keys (1,2 and 3) in the inner individual item arrays of the $list multidimensional array are then set to be the VALUES for the $filter array by becoming the value (i.e the right hand side of the expression) in the $filter[$originalKey] = $item['priority'] expression. But again, remember they are no longer the ACTUAL priority values anymore from the inner arrays of $list. The, $filter array is an entirely separate array from $list and its six values (you will get six values because there are six keys when status is set to 'all') are just copies of the priority key integer values which can only be either 1, 2 or 3.

5) To try and explain this a little more the $item['priority'] values in the inner arrays of the $list array are as follows:

[0] Laundry priority => 2 [1] Dishes priority => 2 [2] Dust priority => 3 [3] Vacuum => 1 [4] Make Dinner => 1 [5] 'Clean Out Fridge => 2

You loseLaundry, Make Dinner and Clean Out Fridge from the $filter array because their keys are 0,4 and 5. Remember the values for $filter can only be 1,2 and 3 because priority was only set to those integers, there is no priority 0,5 and 5. So notice the above value sequence is 2, 2, 3, 1, 1, 2

6) Once an 'all' $filter is created you end up with the following. Remember the $filter keys will be a carbon copy of $list keys and the values will be carbon copies of the priority values so the $filter array content is as follows:

[0] => 2 [1] => 2 [2] => 3 [3] => 1 [4] => 1 [5] => 2

Notice the sequence of the $filter values are exactly the same as the sequence of the inner array priority values within $list (2, 2, 3, 1, 1, 2)

8) The REASON you get that seemingly random order repetitive table displayed in the browser is because the second foreach loop in the code that generates and displays the table uses the VALUES of the filter $array to select the KEY of the $list array.

Remember that if you express foreach with only one new variable expressed, as is the case for ($filter as $id). The new variable is automatically based on the values of the parent array, in this case the parent is $filter so you get a further new array six variables as follows

$id[0] = 2 $id[1] = 2 $id[2] = 3 $id[3] = 1 $id[4] = 1 $id[5] = 2

.Remember that in the absence of a defined key, php auto sets the key as indexed 0,1,2,3,4,5 etc

(aside - you only can create a new associative array with foreach if you express as for $filter as $key => $id)

the new values of this array are in fact going to pull the KEYS (and all the information of the inner array they correspond to) from the $list because the table rows use the integer values created by $id as the keys for $list[$id]['priority']...which in this context means select the $list KEY.

so....when the table finally displays you get a table based on the above array. I.E

$id[0] = 2 (Dust) $id[1] = 2 (Dust) $id[2] = 3 (Vacuum) $id[3] = 1 (Dishes) $id[4] = 1 (Dishes) $id[5] = 2 (Dust)

Don't get thrown by the priority values displayed in the final table (which show as 3, 3, 1, 2, 2, 3), they no longer have any effect on the order of the array, but they do remain the priority values for dust, vacuum and dishes inner arrays. Those values were the starting point, but end up completely unrelated to the order of the final array (until they are sorted by the sort function in the next video).

Hope that helps. A bit. Took me a lot of rewatching the video and about a month to work that all out.