Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP foreach loop example confusion

Tags:

foreach

php

One of the examples of a foreach loop in PHP is

foreach ($row as $key => $value) { 
    // somecode
}

I am having trouble understanding what the => is doing. Can anyone run me through how it is "seen" or evaluated by PHP? What is the order of operation, what value is assigned to $key?

What makes it different from:

foreach ($row as $value) { 
    // somecode
}

?

I logically I thought that the value of $value would be assigned to $key, then it would be assigned as a row of $row, but that is obviously incorrect...

like image 407
Justin Avatar asked May 25 '11 20:05

Justin


People also ask

What is a foreach loop in PHP?

The foreach loop - Loops through a block of code for each element in an array. The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Can You foreach an array in PHP?

[EDIT BY danbrown AT php DOT net: Contains a typofix by (scissor AT phplabs DOT pl) on 30-JAN-2009.] "Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer.

How to modify an array inside a loop in PHP 7?

As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior. In PHP 7, foreach does not use the internal array pointer. In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Can you modify an array in a foreach loop?

Note that foreach does not modify the internal array pointer, which is used by functions such as current () and key () . It is possible to customize object iteration . In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference .


2 Answers

I understand it is a little tricky, I had problems understanding it when I first started using it. The more you use it, the more it makes more sense.

Your array would look like this:

//  "key"   "value"
//    |        |
//    V        V
$row['1'] = 'item1';
$row['2'] = 'item2';
$row['3'] = 'item3';
$row['4'] = 'item4';

Keys can be anything, they don't need to be numbers. That way you can iterate through all items in the array without needing to know the key!

So, your first example can be explained as follows:

//         +--- The ARRAY where all of your data is
//         |
//         |       +----- The KEY to access that element of the array
//         |       |
//         |       |        +----- The VALUE of that element
//         |       |        |
//         V       V        V
foreach ($row as $key => $value){
   if($row[$key] == $value){ // this statement is always true
      echo "true AGAIN!";  // and thus will always print this line
   }
}

As far as my understanding goes the => is not really an operand of sorts, it is just used to complete the structure of the foreach loop.

like image 161
CenterOrbit Avatar answered Sep 25 '22 02:09

CenterOrbit


PHP arrays are stored as pairs of keys and values:

$arr = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

The standard foreach syntax gets just the values:

foreach ($arr as $value) {
    // value1, value2
}

The alternative syntax allows you to get keys as well:

foreach ($arr as $key => $value) {
    // $key is "key1", then "key2"
    // $value is "value1" then "value2"
}

See the manual entry for foreach.

like image 38
lonesomeday Avatar answered Sep 25 '22 02:09

lonesomeday