英文:
links from an array to external variables?
问题
如何从数组中引用外部变量?
例如:如果我改变了数组的值,
那么这些值应该存储在由数组元素引用的变量中。
我尝试实现这个想法,但它没有起作用 :(
根据我的想法:
1. `echo $one` 应该输出 `1`
2. `echo $two` 应该输出 `2`
$one;
$two;
$link = [$first = &$one, $second = &$two];
$link[0] = 1;
$link[1] = 2;
echo $one; // 没有显示 1
echo $two; // 没有显示 2
英文:
How to make references to external variables from an array?
For example: if I change the array values then
these values should be stored in variables
referenced by array elements.
I tried to implement this idea, but it didn't work
According to my idea:
-
echo $one
should output1
-
echo $two
should output2
$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
答案1
得分: 0
你应该按照以下方式结构化你的代码:
$one;
$two;
// 如果你想让它们成为键,你没有初始化$first和$second,应该像这样
//$link = [$first => &$one, $second => &$two];
$link = [&$one, &$two];
$link[0] = 1;
$link[1] = 2;
// 如果你需要将它们初始化为另一个变量,那么应该在你给$link[0]和$link[1]赋值之后执行
$first = $link[0];
$second = $link[1];
echo $one; // 显示 1
echo $two; // 显示 2
echo $first; // 显示 1
echo $second; // 显示 2
英文:
You should structure your code like so:
$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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论