程式碼:
<?php
$a = array( 'A', 'B', 'C' , 'D' );
print_r($a);
foreach( $a as &$v )
$v = strtolower($v);
$v = 123123123;
print_r($a);
結果:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => 123123123
)
可以看到上述的最後一個 element 變成 123123123,而解法就是透過 unset 來處理:
<?php
$a = array( 'A', 'B', 'C' , 'D' );
print_r($a);
foreach( $a as &$v )
$v = strtolower($v);
unset($v);
$v = 123123123;
print_r($a);
結果:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
沒有留言:
張貼留言