php地址和值引用
function test(){ $datas = [100,200,300,400]; foreach ($datas as &$item){ } foreach ($datas as $item){ } dump($datas); } |
输出如下
array(4) {
[0] => int(100)
[1] => int(200)
[2] => int(300)
[3] => &int(300)
}
function testaa(){ $datas = [100,200,300,400]; foreach ($datas as &$item){ } foreach ($datas as $item){ dump($datas); } } |
输出如下
array(4) {
[0] => int(100)
[1] => int(200)
[2] => int(300)
[3] => &int(100)
}
array(4) {
[0] => int(100)
[1] => int(200)
[2] => int(300)
[3] => &int(200)
}
array(4) {
[0] => int(100)
[1] => int(200)
[2] => int(300)
[3] => &int(300)
}
array(4) {
[0] => int(100)
[1] => int(200)
[2] => int(300)
[3] => &int(300)
}
解决方法如下:
function test(){ $datas = [100,200,300,400]; foreach ($datas as &$item){ } foreach ($datas as $itemOrder){ } dump($datas); } |
function test(){ $datas = [100,200,300,400]; foreach ($datas as &$item){ } unset($item); // 取消引用 foreach ($datas as $itemOrder){ } dump($datas); } |
参考地址 https://zhidao.baidu.com/question/2081052214131826308.html