【发布时间】:2021-05-27 15:59:39
【问题描述】:
我有这样的数据:
$a = [
"attr1"=> null,
"list"=> [
[
"other_attr"=> 47,
"personal"=> [
[
"id"=> 3071,
"quantity"=> 4,
"price"=> 340000
]
]
],
[
"other_attr"=> 47,
"group"=> [
[
"id"=> 3086,
"quantity"=> 2,
"price"=> 350000
]
]
]
]];
并具有以下功能:
foreach( $a['list'] as &$value ){
if (!empty($value["personal"])) {
foreach ($value["personal"] as &$item) {
$item['F'] = "TREDA";
}
foreach ($value["personal"] as $item) {
print("do some thing here with new attribute F\n");
}
$value["C"] = 32213;
}
if (!empty($value["group"])) {
foreach ($value['group'] as $item) {
print("Do some thing here\n");
}
$value["C"] = "AAAA";
}
}
$a 的预期结果如下:
{"attr1":null,"list":[{"other_attr":47,"personal":[{"id":3071,"quantity":4,"price":340000,"F":"TREDA"}],"C":32213},{"other_attr":47,"group":[{"id":3086,"quantity":2,"price":350000}],"C":"AAAA"}]}
但实际数据错误如下:
{"attr1":null,"list":[{"other_attr":47,"personal":[{"id":3086,"quantity":2,"price":350000}],"C":32213},{"other_attr":47,"group":[{"id":3086,"quantity":2,"price":350000}],"C":"AAAA"}]}
Id 3071 已替换为 Id 3086。
谁能给我解释一下这里有什么问题?
感谢您的帮助!
链接PHPOnline Sandbox中的P/S代码演示
【问题讨论】:
-
您需要在
$value['personal']的第一个foreach循环中更新/修改$item后添加unset($item); -
在提供的沙盒链接上更新
-
引用PHP.net:
Warning Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset(). Otherwise you will experience the following behavior -
不幸的是,PHP 不维护循环内变量的局部范围(例如,与 Java 不同)。请参阅示例:sandbox.onlinephpfunctions.com/code/… 因此,进一步引用同一变量可能会导致代码中出现未定义的行为。