& Amp; lt ;? Php $ countries [{code...}]; foreach ($ countriesas $ key & amp; gt; $ value) {code ...}}? & Amp; gt;
$ Countries = [
[ '0' => [ 'id' =>0 ] ], [ '1' => [ 'id' =>1 ] ],
];
Foreach ($ countries as $ key => $ value ){
Print_r (current ($ countries); echo 'next'; print_r (pos ($ countries); next ($ countries); echo PHP_EOL;
}
?>
Reply content:
$ Countries = [
[ '0' => [ 'id' =>0 ] ], [ '1' => [ 'id' =>1 ] ],
];
Foreach ($ countries as $ key => $ value ){
Print_r (current ($ countries); echo 'next'; print_r (pos ($ countries); next ($ countries); echo PHP_EOL;
}
?>
Defined$countries
And in the foreach loop$countries
Point to the same zval variable, because PHP needs to save memory, and does not need to store the same data in two copies. In this caserefcount
2. but if it is changed in the loop$countries
For example
Foreach ($ countries as & $ country) {$ country = 'China'; // because the value assignment operation is performed here, copy-on-write}
Or
Foreach ($ countries as $ country) {echo current ($ countries); // because current is to be referenced, copy-on-write} occurs here}
Passed the reference and assigned a value, which is triggeredcopy-on-write
Operation, that is, copy the zval to the original zvalrefcount
Minus 1.
The current statement is executed every time a loop occurs.copy-on-write
So each current operation is a new zval.
Same as this answer