我在生成器中遇到了这种奇怪的行为,PHP 手册中没有对此进行描述。
我有两个 foreach 循环 - 第一个设置一些默认值,第二个覆盖它们。但由于缺少一些键:值对,我无法使用 array_merge(),因此我尝试使用生成器来实现此目的。
我发现即使docs say,yield 键也会在关联数组中被覆盖。 :
The syntax for yielding a key/value pair is very similar to that used to define an associative array, as shown below.
示例:
function yieldTest()
{
// those are array values (in my code - taken from data source; here static example)
$arr1 = ['a' => 1, 'b' => 2, 'c' => 3];
$arr2 = ['b' => 'B', 'd' => 'D'];
// 1st loop
foreach ($arr1 as $k => $v) {
yield $k => $v;
}
// 2nd loop
foreach ($arr2 as $k => $v) {
yield $k=>$v;
}
}
foreach(yieldTest() as $k=>$v) {
var_dump($k . ' = ' . $v) . "\n";
}
结果是
string(5) "a = 1"
string(5) "b = 2"
string(5) "c = 3"
string(5) "b = B"
string(5) "d = D"
所以 key 没有被覆盖。我期望得到以下输出:
string(5) "a = 1"
string(5) "b = B"
string(5) "c = 3"
string(5) "d = D"
这是正确的行为吗?
最佳答案
生成器不是关联数组。它说“[t]用于生成键/值对的语法与[..]关联数组非常相似”;它根本没有说生成器的行为类似于关联数组。
事实上,它不能表现出相同的行为并删除重复的键,因为这些键不能同时被识别。每个 key 都是在需要时生成的,而不是之前生成的。用于循环生成器的 foreach
语法实际上是这样的语法糖:
function foo() {
while (true) {
yield mt_rand(1, 2) => 'foo';
}
}
$foo = foo();
$foo->next();
echo $foo->key(), ' => ', $foo->current(), PHP_EOL;
$foo->next();
echo $foo->key(), ' => ', $foo->current(), PHP_EOL;
$foo->next();
echo $foo->key(), ' => ', $foo->current(), PHP_EOL;
$foo->next();
echo $foo->key(), ' => ', $foo->current(), PHP_EOL;
This will yield the same key many times, unpredictably.很明显,它的行为根本不像数组。它只产生一对值,但它们根本不是数组的一部分,因此不会进行重复数据删除。此外,生成器仅在要求时生成下一个值,否则上述将导致无限循环。
关于php - 为什么 'yield' 不会覆盖 PHP 中的键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33303986/