foreach中引用
$arr = [
0 => ['name' => '张三', 'age' => 22, 'sex' => '男'],
1 => ['name' => '小红', 'age' => 20, 'sex' => '女']
];
//没引用,值不改变
foreach ($arr as $v) {
$v['name'] = '李四';
}
print_r($arr);
//使用key对数组重新赋值
foreach ($arr as $key => $v) {
if ($v['name'] == '张三') {
$arr[$key]['name'] = '李四';
}
}
print_r($arr);
//有引用,值改变
foreach ($arr as &$v) {
if ($v['name'] == '张三') {
$v['name'] = '李四';
}
}
print_r($arr);