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 Random Fun with Arrays

Teacher Russell
Teacher Russell
16,873 Points

random fun with arrays

Can someone offer someone offer some explanation of what's happening in this video around 3:00? Too many big changes too quickly and I can't quite figure out why or what:) Thank you!

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there,

Usually, when you're struggling because it's seemingly happening too fast, it's a good time to watch the video again... you might begin to absorb a little more when you know what to expect in the video.

But I'll try to explain what's going on :)

We're basically iterating through an array with PHP and displaying the results in the browser.

get_item_html() might not seem likee it's doing anything much but we're echoing out the results of a function call.

Every time the loop iterates (which means runs a single cycle through the loop) it will return different items from an array. That's what the 4 does in the rand function. It's limiting the display for 4 random items in the array.

<?php

get_item_html($id, $catalog[$id])

?>
Teacher Russell
Teacher Russell
16,873 Points

Yes, I've got all that. I understand what the random function does, how our custom function...functions basically. I was looking for a little more detailed explanation of how that's happening, with the final modification shown above. What exactly is happening when passing the $id, then the entire array with $id. I can recite the video by heart:) I just don't see this part of it clearly. Step by step what's happening after the last step. When I say there's a lot happening too quickly, I mean a lot seems to happen before it's explained, before the end of the video.