php - 嵌套数组:只有内部引用时访问父数组范围

问题描述 投票:0回答:1

我们来看看代码:

function update_by_ref(&$ref)
{
    $ref[] = 2;
    $ref[] = 3;        
}

$a = [];
$a_ref = &$a;

$b = [];
$b[] = &$a_ref;

update_by_ref($a_ref);
print_r ($b);

结果数组:

[[2,3]]

是否有任何选项可以在update_by_ref()函数中修改原始数组并获得如下内容:

[[2,3], [4,5]]
php reference pass-by-reference
1个回答
0
投票

不,这是不可能的。

子元素没有关于父范围的信息,可能没有父范围。

© www.soinside.com 2019 - 2024. All rights reserved.