So I have an index.php file that has data.php (array list of different types of items):
$catalog[304] = [
"title" => "The Very Thought of You",
"img" => "img/media/nat_king_cole.jpg",
"genre" => "Jaz",
"format" => "MP3",
"year" => 2008,
"category" => "Music",
"artist" => "Nat King Cole"
]; //and so on ->
I also have a function.php file that hold this function:
function get_item_html($id, $item) {
$output =
"<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p> View Details </p>"
. "</a></li>";
return $output;
And then the index.php which have both files included at the top:
<ul class="items">
$random = array_rand($catalog, 4);
foreach ($random as $id) {
echo get_item_html($id, $catalog[$id]);
}
What I can't grasp is the "$catalog[$id]". It seems like it should return the catalog ID, not the key within the catalog ID. But apparently the code is valid, because it does work. It's just that I can't understand why.
I know that I am way off in this but if someone could explain it to me I would be super grateful.
Thanks!