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 trialFilipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 PointsRandom Fun with Arrays
I couldn't understand the use of $id in this process. It just made me confused and loose understanding of the whole process of adding $random in the foreach. Could someone please explain me the reason of each step she took?
3 Answers
christian gamboa
10,097 PointsIn reference to your most recent question: The function : get_item_html($id, $item); takes any 2 parameters. Remember the data from data.php looks like this:
$catalog[101] =["img" => "../path/to/the/image.jpg"];
Ultimately the goal is to retrieve the path to the image using a random $id (In my example above the $id = 101). So in order to access the data you can do something like this: catalog[101]['img'] and you'll get ../path/to/the/image.jpg. Now check functions.php and look how they use $item["img"] to display the html. All you need to do is to pass catalog[101] instead of $item so that you can get the path.
Pay close attention to the way the arrays in data.php are arranged. First Authors Name : echo $catalog['any random id']['authors'][0] Play with some arrays :)
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 PointsThanks, mate. It really helped me.
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 PointsHey, christian gamboa , I only saw your comment today. Didn't get any notice. I'm still a little bit confused. From what I see, $catalog[$id] replaces the $item. Would it be right to say that $catalog[$id] = $item from the function? I still don't understand why it has to be that way.
Bruno Dias
10,554 PointsI'm very confused as well. It was so easy to follow Alena Holligan but this lesson wasn't very clear unfortunately. I think she could go more deeply into what she had done here.
I was able to follow this part:
$random = array_rand($catalog, 4);
However the way we call get_item_html
function made me very confused. I am still unable to understand what's happening here.
foreach($random as $id) {
echo get_item_html($id, $catalog[$id]);
}
Can anyone clarify, please?
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 PointsHey Bruno Degaspare
One thing that helped. Try to var_dump() the $catalog and then $random, and try to compare them. You will see that they are quite different. The var_dump($catalog) will display all the informations inside the $catalog array, while the var_dump($random) will only display 4 random values. These values are the keys of each $catalogue array, therefore they are the $id.
So now, in the foreach loop, you can only pass that one value, so you do foreach($random as $id).
Now, remember that the get_item_html function needs to have 2 parameters? In this case it will be the $id. But what do you do with the $item that was needed, but can't be passed in the loop? If you look more into arrays, you will realize that $catalog[$id] is the same thing as the $item in the function, so you use that.
I don't know if it will help you. Christian's explanation helped me a lot, but I could only understand better when I spent a LOT of time studying and playing with arrays and foreach loop on my own. What I've realised in Alena's teachings is that she explains well how to do the things, but if you want to understand why each thing should be done in that specific way, you gotta work hard and find out yourself.
christian gamboa
10,097 Pointschristian gamboa
10,097 PointsInitially you display the entire catalog. Now you want to randomly display only four items from the catalog. She uses the array_rand() function to achieve that. Your new array is now $random with 4 id numbers. Using get_item_html function, pass the $id back to the $catalog *REMEMBER data.php contains all the arrays like $catalog[101] or $catalog[102] where 101 and 102 are the id numbers to identify each $item in the $catalog* . I suggest to use the var_dump() as much as possible.
I hope it helps.