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 trialAlexander Bogdanov
2,587 PointsWhat does $output[$id] = $sort; do?
I can't understand what the following code does: $output[$id] = $sort; thanks for future help!
4 Answers
Cody Hansen
5,517 PointsIn our data.php file, we can see that our $catalog array is filled with other arrays containing information on a book, movie, or music. In a previous video, it was said that we are using 101, 102, 103, etc. for books, 201, 202, etc. for movies and so on. These numbers are our keys. Our keys are what we are referring to with the $id variable.
In the video, $sort was set to our current item's title. So let's use an example!
Let's say $sort gets set to "Drive". "Drive" is a movie, so the key used in the array the foreach is going through could be 203. Therefore, $id will be 203.
So, $output[$id] = $sort; actually means:
$output[203] = "Drive";
Meaning that at 203 in the $output array the string "Drive" is stored.
I'm not the best at explaining, so if you need any more help I will do my best to rewrite it. Hope this helped. Good luck!
Robyn Gamm
4,408 PointsThat did help, thank you Cody!
julian Ellis
3,542 PointsOn the line below are we passing both the current item;s title AND its key to $sort?
$sort = $key["title"];
Jeremiah Allen
14,627 PointsI think the foreach is essentially trimming down $catalog array so that it only holds the $items 'title's while retaining the index $id. $output[ $id (key of item) ] = $sort (title value of item)
then asort can sort by value, which is now the title
asort($output);
and return the keys in the new order
return array_keys($output);
jlampstack
23,932 PointsAdding to Cody's great explanation, a simple var_dump() can help you see what is going on beneath the surface.
Original $output
foreach($catalog as $id => $item){
if(strtolower($category) == strtolower($item['category'])) {
$output[] = $id;
}
}
var_dump($output); // Returns the following ....
// array(4) { [0]=> int(201) [1]=> int(202) [2]=> int(203) [3]=> int(204) }
Sorted $output BEFORE using array_keys();
foreach($catalog as $id => $item){
if(strtolower($category) == strtolower($item['category'])) {
$sort = $item['title'];
$output[$id] = $sort;
}
}
var_dump($output); // Returns the following ....
// array(4) { [201]=> string(12) "Forrest Gump" [202]=> string(12) "Office Space" [203]=> string(49) "The Lord of the Rings: The Fellowship of the Ring" [204]=> string(18) "The Princess Bride" }
Sorted $output AFTER using array_keys();
foreach($catalog as $id => $item){
if(strtolower($category) == strtolower($item['category'])) {
$sort = $item['title'];
$output[$id] = $sort;
}
}
var_dump(array_keys($output)); // Returns the following ....
// array(4) { [0]=> int(201) [1]=> int(202) [2]=> int(203) [3]=> int(204) }
Cheryl Oliver
21,676 PointsCheryl Oliver
21,676 PointsReally great explanation ;)