0

I understand that => operator in PHP is used to assign values in an associative array:

$array = array(key1 => value1, key2 => value2, key3=> value3);

I understand that for-each loop in PHP is iterated like

foreach ($array as $value) {
  echo $value;
}

But I have encountered something like

foreach ($question->get_order($qa) as $value => $ansid) {...}

I do not understand the $value => $ansid part of it.

$question -> get_order($qa) returns an array. We want to iterate through it, so it should be foreach ($question -> get_order($qa) as $value) {...}?

Solace
  • 8,612
  • 22
  • 95
  • 183
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) –  Jul 14 '14 at 20:18

3 Answers3

5

The => operator assigns the keys of the array to the variable on the left hand side, and the value to the variable on the right hand side. For example, if you array is

$array = array(key1 => value1, key2 => value2, key3=> value3);

then

foreach ($array as $key => $value) {
  echo "$key: $value\n";
}

will print

key1: value1
key2: value2
key3: value3

It is particularly useful if your array keys also have a meaning and you need them inside the for-loop, separate from the values.

For example:

$students_by_id = array( 1234 => "John Smith", 2372 => "Pete Johnson" );
$grades = array( 1234 => 87, 2372 => 68 );

foreach( $grades as $student_id => $grade ) {
  echo $students_by_id[$student_id] . " scored " . $grade . " / 100 points.\n";
}

Note that if the array is "not associative", e.g.

$array = array( value1, value2, value3 );

then PHP will create numeric indexes for you, and the $key variable in

foreach ($array as $key => $value )

will run through 0, 1, 2, 3, ..., making your loop effectively equivalent to

for ($key = 0, $key < count($array); ++$key) {
  $value = $array[$key];
  // ...
}

In general I would still recommend the => notation, if not for efficiency then at least in case indices go missing from the list or you decide to switch to an associative array after all.

CompuChip
  • 9,143
  • 4
  • 24
  • 48
4

In a for loop, you can use the same operator to get the keys as well as the values. The variable before => will get the key of each item, and the variable behind it will get its value.

So in your specific case, $value will get the key of the item ('key1' on the first iteration) and $ansid will get the value ('value1' on the first iteration).

This feature is particularly useful for arrays with (named) keys, but it will also work for normal arrays, in which case you get the numeric indices for the keys.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
2

The $value => $ansid will return the key and the value, not just the value.

So if its a plain array, key would likely be 0,1,2,3,4 etc and value would be v0,v1,v2,v3,v4.

beiller
  • 3,105
  • 1
  • 11
  • 19
  • 3
    This could be improved if you used their sample array. – Patrick James McDougle Jul 14 '14 at 20:08
  • `$question = get_order($qa)` returns an indexed array, not an associative array. So should I learn (from your answer) that `$value` will get the indices that is 0,1,2,3... and `$ansid` will get the actual contents of the array? – Solace Jul 14 '14 at 20:17
  • 1
    Yes, if the array is indexed, then the indices are automatically numbered 0, 1, 2, 3, ... It is similar as if you would have written `array( 0 => first, 1 => second, ...)` yourself, rather than `array( first, second, ...)`. – CompuChip Jul 14 '14 at 20:26
  • 1
    Yes your question gets slight originality points, because as we see it will automatically index the key as the array index for plain arrays in PHP. – beiller Jul 14 '14 at 20:28