4 回答

TA貢獻1784條經驗 獲得超7個贊
$a = array(1,2,3);$b = $a; // This is lazy cloning of $a. For the time // being $a and $b point to the same internal // data structure.$a[] = 3; // Here $a changes, which triggers the actual // cloning. From now on, $a and $b are two // different data structures. The same would // happen if there were a change in $b.
foreach
$b

TA貢獻1797條經驗 獲得超6個贊
foreach()
:
foreach
foreach()
prospected copy
copy-on-write
foreach()
foreach()
DISTINCT SENTINEL VARIABLES
foreach
SENTINEL
(for example, the current index variable)
foreach()
$array = array(1, 2, 3, 4, 5);list($key2, $val2) = each($array);echo "each() Original (outside): $key2 => $val2<br/>"; foreach($array as $key => $val){ echo "foreach: $key => $val<br/>"; list($key2,$val2) = each($array); echo "each() Original(inside): $key2 => $val2<br/>"; echo "--------Iteration--------<br/>"; if ($key == 3){ echo "Resetting original array pointer<br/>"; reset($array); }}list($key2, $val2) = each($array);echo "each() Original (outside): $key2 => $val2<br/>";
產出:
each() Original (outside): 0 => 1
foreach: 0 => 1
each() Original(inside): 1 => 2
--------Iteration--------
foreach: 1 => 2
each() Original(inside): 2 => 3
--------Iteration--------
foreach: 2 => 3
each() Original(inside): 3 => 4
--------Iteration--------
foreach: 3 => 4
each() Original(inside): 4 => 5
--------Iteration--------
Resetting original array pointer
foreach: 4 => 5
each() Original(inside): 0=>1
--------Iteration--------
each() Original (outside): 1 => 2
- 4 回答
- 0 關注
- 506 瀏覽
添加回答
舉報