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 trial

PHP Build a Simple PHP Application Integrating with PayPal Including the Products Array

Where does the shirts gets it id?

I see the page display each id of the shirt when clicked, but where does price/product_id comes from? and why are we using => in the loop?

2 Answers

Nicklas Wiborg
Nicklas Wiborg
12,692 Points

the => in the loop creates a variable with the key of the item in the array.

if you have:

$array = new array("first","second");

You have a array with the following:

$array[0] = "first";
$array[1] = "second";

If we loop trough this array with the foreach-loop, like this:

foreach($array as $item_key => $item){

}

$item_key will be assigned the key of the array-item which is in this case 0 for "first" and 1 for "second".

Nicklas Wiborg
Nicklas Wiborg
12,692 Points

If I extend my answer:

foreach($array as $item_key => $item){
    echo $item_key;
}

Will return "01";

you mean like if we have a array like '''html $product = array(); $product['101'] => apple; ''' so when we do foreach loop, with $key => $value , the $key will become 101?

p.s - How do you format boxes for code?

It's in the Markdown Cheatsheet right below here.

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

        ```html
        <p>This is code!</p>
        ```
Nicklas Wiborg
Nicklas Wiborg
12,692 Points

Yes! If you have a properly functioning array it will.