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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Sorting Array Items

Alexander Bogdanov
Alexander Bogdanov
2,587 Points

What does $output[$id] = $sort; do?

I can't understand what the following code does: $output[$id] = $sort; thanks for future help!

4 Answers

In 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!

Cheryl Oliver
Cheryl Oliver
21,676 Points

Really great explanation ;)

That did help, thank you Cody!

julian Ellis
julian Ellis
3,542 Points

On the line below are we passing both the current item;s title AND its key to $sort?

$sort = $key["title"];

Jeremiah Allen
Jeremiah Allen
14,627 Points

I 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);

Adding 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) }