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 trialKer Sing Tan
10,573 Pointswhy need to change from $item to $catalog[$id$] ?
Why need to change the echo from ($random as $id => $item) to ($random as $catalog[$id]) ?
2 Answers
marcell gibbs
3,744 PointsI believe it is because the array_rand() function returns the key of an array or more specifically in this case an array of keys since the number is specified. So to access that actual value we would need to get $catalog[$id];
michaeldavies3
23,152 Points$random gives an array of 4 randomly chosen "keys" from the $catalog array, e.g. "101" , not the data attached to that key (this is the "value").
To get the "value" for each key in the array, we need to do an array lookup for each keys' data in the catalog array. Array lookups are done using square brackets, which is how we get to $catalog[$id].
Hope that helps!