提问人:Homersx 提问时间:1/6/2023 最后编辑:user3783243Homersx 更新时间:1/8/2023 访问量:50
从数组到外部变量的链接?
links from an array to external variables?
问:
如何从数组中引用外部变量? 例如:如果我更改数组值,那么 这些值应存储在变量中 由数组元素引用。 我试图实现这个想法,但它没有奏效:(
根据我的想法:
echo $one
应该输出1
echo $two
应该输出2
$one; $two; $link = [$first = &$one, $second = &$two]; $link[0] = 1; $link[1] = 2; echo $one; // does not show 1 echo $two; // does not show 2
答:
0赞
Wisdom Ighofose
1/6/2023
#1
您应该像这样构建代码:
$one;
$two;
//you did not initialize the $first and $second if you want them to be the keys like so
//$link = [$first => &$one, $second => &$two];
$link = [&$one, &$two];
$link[0] = 1;
$link[1] = 2;
//if you need to initialize them to another variable then do
//And it should come after you have assigned a value to $link[0] and $link[1]
$first = $link[0];
$second = $link[1];
echo $one; // show 1
echo $two; // show 2
echo $first; // show 1
echo $second; // show 2
评论
0赞
Homersx
1/7/2023
我想将该选项与附加变量一起使用,但发生了错误。这是行不通的。$one; $two; $link = [$first => &$one, $second => &$two]; $first = $link[0]; $second = $link[1]; $link[0] = 1; $link[1] = 2; echo $one; echo $two;
0赞
Wisdom Ighofose
1/8/2023
像这样设置你的数组:make 和数组键,而不是初始化变量。$link = [$first => &$one, $second => &$two];
$first
$second
0赞
Wisdom Ighofose
1/8/2023
如果您需要将 和 分配给数组项并获取您刚刚分配的值(),请查看我分配的编辑答案以及设置数组项的值后$first
$second
$one
$two
$link[0] = 1; $link[1] = 2;
$first = $link[0];
$second = $link[1];
评论
$first = &$one, $second = &$two
$link = [&$one, &$two];