简体   繁体   English

for-each循环中的=>运算符是什么意思?

[英]What does the => operator in the for-each loop mean?

I understand that => operator in PHP is used to assign values in an associative array: 我知道PHP中的=>运算符用于在关联数组中分配值:

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

I understand that for-each loop in PHP is iterated like 我知道PHP中的for-each loop

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. 我不了解其中的$value => $ansid部分。

$question -> get_order($qa) returns an array. $question -> get_order($qa)返回一个数组。 We want to iterate through it, so it should be foreach ($question -> get_order($qa) as $value) {...} ? 我们要遍历它,所以应该是foreach ($question -> get_order($qa) as $value) {...}

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 -loop中将它们与值分开,则它特别有用。

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", eg 请注意,如果数组是“非关联的”,例如

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

then PHP will create numeric indexes for you, and the $key variable in 然后PHP将为您创建数字索引,并在其中创建$key变量

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

will run through 0, 1, 2, 3, ..., making your loop effectively equivalent to 将运行0、1、2、3,...,从而使循环等效于

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. 通常,我仍然建议使用=>表示法,如果不是出于效率考虑,则至少在列表中缺少索引的情况下,或者您毕竟决定切换到关联数组。

In a for loop, you can use the same operator to get the keys as well as the values. 在for循环中,可以使用相同的运算符来获取键和值。 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). 因此,在您的特定情况下, $value将获取项目的键(在第一次迭代中为'key1' ),而$ansid将获得值(在第一次迭代中为'value1' )。

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. 此功能对具有(命名)键的数组特别有用,但是它也适用于普通数组,在这种情况下,您将获得键的数字索引。

The $value => $ansid will return the key and the value, not just the value. $ value => $ ansid将返回键和值,而不仅仅是值。

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. 因此,如果其为纯数组,则键可能为0、1、2、3、4等,并且值将为v0,v1,v2,v3,v4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM