Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

1)In foreach use $key => $value

foreach($featured as $key => $value){   echo $value['name']; } 

this outputs the same as:

2)In foreach use only $value

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.

like image 530
handmdmr Avatar asked Aug 28 '13 15:08

handmdmr


People also ask

What is $key in foreach?

On each iteration, the value of the current element is assigned to $value . The second form will additionally assign the current element's key to the $key variable on each iteration. Note that foreach does not modify the internal array pointer, which is used by functions such as current() and key().

What does => mean in PHP array?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

What is the difference between MAP and foreach?

Differences between forEach() and map() methods:The forEach() method does not create a new array based on the given array. The map() method creates an entirely new array. The forEach() method returns “undefined“. The map() method returns the newly created array according to the provided callback function.

What is the syntax of the foreach loop in case of associative array?

The foreach loop is mainly used for looping through the values of an array. It loops over the array, and each value for the current array element is assigned to $value, and the array pointer is advanced by one to go the next element in the array. Syntax: <?


Video Answer


2 Answers

Well, the $key => $value in the foreach loop refers to the key-value pairs in associative arrays, where the key serves as the index to determine the value instead of a number like 0,1,2,... In PHP, associative arrays look like this:

$featured = array('key1' => 'value1', 'key2' => 'value2', etc.); 

In the PHP code: $featured is the associative array being looped through, and as $key => $value means that each time the loop runs and selects a key-value pair from the array, it stores the key in the local $key variable to use inside the loop block and the value in the local $value variable. So for our example array above, the foreach loop would reach the first key-value pair, and if you specified as $key => $value, it would store 'key1' in the $key variable and 'value1' in the $value variable.

Since you don't use the $key variable inside your loop block, adding it or removing it doesn't change the output of the loop, but it's best to include the key-value pair to show that it's an associative array.

Also note that the as $key => $value designation is arbitrary. You could replace that with as $foo => $bar and it would work fine as long as you changed the variable references inside the loop block to the new variables, $foo and $bar. But making them $key and $value helps to keep track of what they mean.

like image 116
ajiang Avatar answered Sep 29 '22 23:09

ajiang


Let's say you have an associative array like this:

$a = array(     "one" => 1,     "two" => 2,     "three" => 3,     "seventeen" => array('x'=>123) ); 

In the first iteration : $key="one" and $value=1.

Sometimes you need this key ,if you want only the value , you can avoid using it.

In the last iteration : $key='seventeen' and $value = array('x'=>123) so to get value of the first element in this array value, you need a key, x in this case: $value['x'] =123.

like image 35
Charaf JRA Avatar answered Sep 29 '22 23:09

Charaf JRA