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

Amber Hoddinott
Amber Hoddinott
6,494 Points

I need some help understanding that last part fo this code please.

Ok so first i want to say im thinking that im over thinking this and im sorry if this end up being a silly question.

Firstly ill explain my understanding so far before i ask my questions just to be sure that im understanding this code correctly.

so the $random variable is holding a value of the new array which has been created by the array_rand() function. This function has the keys in this case 102,103,104 and 303 as it values.

when the foreach loop runs it gets in-turn as it runs through the array a variable name of $id. and this is where my understanding stops.

i guess i get that the foreach loop is echoing out the HTML in turn each time it hits the different values of the array, which with this makes the 4 random images ect appear in the browser, that i get. but how i dont get?

Dont understand it where the function_item_html() is getting this information from (which file?) i assume when it echos out that is making the function run? why do we need to pass in ($id,catalog $id) into the functions prentices.

how in having the values for example (102, catalog[102]) in the prentices making the function display the html.

i hope i have explained my understanding and my questions well enough. Thank you in advance to anyone who can help me understand this :)

can you please post the code block you are having issues with.

2 Answers

Lyle Lewton
PRO
Lyle Lewton
Pro Student 10,585 Points

I would be willing to bet you are now well beyond having trouble with this code since this question is a year old, but I'm going to write out an explanation to help my understanding and to help anyone who stumbles upon this question.

$random is an array that will have 4 items, with the values set to 4 random keys from the catalog array. By removing "$item" from the foreach loop "$id" is now representing the value of the $random array instead of the key.

Then we pass the get_item_html function the values from $random (which is the 4 random keys from the catalog array) and the corresponding media item arrays from the catalog array. Then it just displays 4 random items in the same way it displayed all 12 before.

To explain this in more concrete terms, let's say that $random has 101 as it's first value and lets just analyze what happens with that first value. It would be in the array as 0 => 101 so I'm going to try to give a visual code representation of what's happening:

<?php

//$id would represent 101 since in the $random array it would be 0 => 101
foreach($random as $id){
echo get_item_html(101, $catalog[101]);
}
?>

These arguments get passed to the function from the functions.php file and the information for the arguments comes from the data.php file which contains the catalog array.

I hope that explanation helps someone and that I didn't just make it sound more confusing!

Amber Hoddinott
Amber Hoddinott
6,494 Points
<?php

$random = array_rand($catalog,4);

foreach($random as $id){
echo get_item_html($id, $catalog[$id]);
}
?>

edited for formatting.