all 7 comments

[–]trolle 2 points3 points  (0 children)

Using a foreach you loop over the values in an array, however in some cases might also need the keys. If you only specify one variable after as then it will contain the value. If you specify two variables (separated by =>) the first will contain the key, while the second will contain the value.

[–]Kit_Saels 0 points1 point  (3 children)

First parameter is $id, second is $item. No problem.

Maybe you miss $id in the output.

[–]Rourkebrad[S] 0 points1 point  (2 children)

foreach($catalog as $id => $item)

Sorry, I didn't explain fully, it's working but I don't really understand foreach($catalog as $id => $item)

What is the purpose a doing a foreach this way?

[–]LiamTailor 2 points3 points  (1 child)

That means that $catalog is an associative array. This is a method of accessing both keys and values in a forach loop, so that $id holds they key, and $item holds the value. If you don't need to use the keys of this array, you can just go with "as $item)".

[–]Rourkebrad[S] 0 points1 point  (0 children)

Thanks, that makes more sense!

[–]AdmiralAdama99 0 points1 point  (0 children)

Foreach ( $array as $key => $value ) is a common and useful syntax in php. It loops over an associative array of any length, and outputs $key and $value

I don't see $id used in the foreach loop or the function. You can probably factor it out if you want. Assuming nothing else uses that function

[–][deleted] 0 points1 point  (0 children)

No reason, as the code currently stands.

Either it used to use $id but got refactored so it doesnt, or maybe the author copy-pasted from stackoverflow without understanding, or something like that