Difference between "as $key => $value" and "as $value" in PHP foreach
I have a database call and I'm trying to figure out what the $key =>
$value does in a foreach loop.
The reason I ask is because both these codes output the same thing, so I'm
trying to understand why it's written this way. Here's the code:
foreach($featured as $key => $value){
echo $value['name'];
}
this outputs the same as:
foreach($featured as $value) {
echo $value['name']
}
So my question is, what is the difference between $key => $value or just
$value in the foreach loop. The array is multidimensional if that makes a
difference, I just want to know why to pass $key to $value in the foreach
loop.
No comments:
Post a Comment