Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Random Fun with Arrays

Teacher Russell
Teacher Russell
16,872 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 89,887 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,872 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.