foreach循环使用&(按址传递)出现的问题原创
# foreach循环使用&(按址传递)出现的问题
在使用foreach引用地址时,一定要在循环结束后使用unset函数将变量销毁。 因为我没销毁变量,导致后面循环的变量一直在覆盖之前的变量。
# 示例代码
$map = [
'all' => [
'id' => 0,
],
'other' => [
'id' => 1,
],
];
$order = [
[
["id" => 1167, "price" => "86400.00", 'supplier_id' => 1],
["id" => 1420, "price" => "990.00", 'supplier_id' => 1],
["id" => 1488, "price" => "2.00", 'supplier_id' => 1],
["id" => 1442, "price" => "150.00", 'supplier_id' => 1],
["id" => 1482, "price" => "150000.00", 'supplier_id' => 1],
["id" => 1485, "price" => "150000.00", 'supplier_id' => 1],
["id" => 1494, "price" => "5000.00", 'supplier_id' => 2],
["id" => 1496, "price" => "2700.00", 'supplier_id' => 2],
],
[
["id" => 1499, "price" => "2200.00", 'supplier_id' => 3],
["id" => 1452, "price" => "6000.00", 'supplier_id' => 3],
["id" => 1455, "price" => "5500.00", 'supplier_id' => 3],
["id" => 1479, "price" => "110000.00", 'supplier_id' => 3],
["id" => 1415, "price" => "1153176.36", 'supplier_id' => 3],
["id" => 1416, "price" => "369555.20", 'supplier_id' => 1],
["id" => 1417, "price" => "135200.00", 'supplier_id' => 1],
["id" => 1448, "price" => "9700.00", 'supplier_id' => 1],
["id" => 1466, "price" => "125.50", 'supplier_id' => 2],
["id" => 1472, "price" => "4502.00", 'supplier_id' => 2],
["id" => 1478, "price" => "4535.00", 'supplier_id' => 2],
["id" => 1491, "price" => "3198.00", 'supplier_id' => 2],
]
];
foreach ($map as $key_map => $value_map) {
//区域ID
$area_id = $value_map['id'];
$order_list = isset($order[$area_id]) ? $order[$area_id] : [];
//供应商表
$supplier_map = [];
foreach ($order_list as $value) {
$supplier_id = $value['supplier_id'];
if (isset($supplier_map[$supplier_id])) {
$supplier_map[$supplier_id]['price'] += $value['price'];
} else {
$supplier_map[$supplier_id] = [
'price' => $value['price']
];
}
}
foreach ($supplier_map as &$value) {
//执行其他的处理
}
$map[$key_map]['list']=$supplier_map;
}
echo json_encode($map);console.log(actor3.name) // 王五
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 预计结果
{
"all":{
"id":0,
"list":{
"1":{
"price":387542
},
"2":{
"price":7700
}
}
},
"other":{
"id":1,
"list":{
"3":{
"price":1276876.36
},
"1":{
"price":514455.2
},
"2":{
"price":12360.5
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 实际结果
{
"all":{
"id":0,
"list":{
"1":{
"price":387542
},
"2":{
"id":1491,
"price":"3198.00",
"supplier_id":2
}
}
},
"other":{
"id":1,
"list":{
"3":{
"price":1276876.36
},
"1":{
"price":514455.2
},
"2":{
"price":12360.5
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 产生原因
内层foreach因为$value变量在按址引用使用后,并没有销毁掉,随即进入下一次的外层foreach循环中。
在下一次的循环中,又有foreach使用$value,循环中将子元素直接赋值给$value变量的物理地址,导致每次循环都会将值给覆盖掉,最终导致数组最后一个元素和下一轮的forache最后一个元素是一致的。
# 解决方法
- 在按址引用使用后,使用unset()函数进行内存销毁。
- foreach循环接收子元素的变量取名不重复。
# 修改后的示例代码
//1、接收变量取名不重复
foreach ($supplier_map as &$supplier_map_value) {
//执行其他的处理
}
//2、使用unset销毁变量
//unset($value);
1
2
3
4
5
6
7
2
3
4
5
6
7
上次更新: 2023/03/20, 21:31:01